Solved: scanf usage

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

SuitedMonkey

Thread Starter
Joined
Nov 10, 2012
Messages
51
so i'm trying to get the user to enter 3 number in one go, is that possible?

Code:
printf("Please enter the time in a 24 hours format \n"); //hours minutes seconds
scand("%d %d %d" &clock1H, &clock1M, &clock1S);
something like that, the only problem is that it's assigning them to random spots in memory and i have no idea why...
 
Joined
Jan 20, 2002
Messages
433
This should be possible. Where did you define clock1H etc?
Also, you have some syntax errors in the code, so please copy the actual code; otherwise, we may not have the right information to help you.
 

SuitedMonkey

Thread Starter
Joined
Nov 10, 2012
Messages
51
Code:
#include <stdio.h>
void main(){
int clock1H; //hours for clock 1
int clock1M; //minute for clock 1
int clock1S; // seconds for clock 1
int clock2H; //hours for for clock 2
int clock2M; //minutes for clock 2
int clock2S; //seconds for clock 2
int clock3H; //hours for clock 3
int clock3M; //minute for clock 3
int clock3S; // seconds for clock 3
char operator; //determines whether addition or subtracting will be used.

printf("Please enter the time in 24 hour format for clock 1 in hour, minute, seconds \n");
scanf("%d:%d:%d,", &clock1H, &clock1M, &clock1S);

printf("Please enter the time in 24 hour format for clock 2 in hour, minute, seconds \n");
scanf("%d:%d:%d", &clock2H, &clock2M, &clock2S);

printf("Please enter whether you want to + or - the clocks. \n");
scanf("%c", &operator);

//printf("Test One, Before If\n");
        if (operator == '+'){
        printf("plus selected\n");
                clock3S == clock2S + clock1S;
                if (clock3S >= 60){
                        clock3M++;
                        clock3S -= 60;
                        }
                clock3M == clock2M + clock1M;
                if (clock3M >= 60){
                        clock3H++;
                        clock3M -= 60;
                        }
                clock3H == clock2H + clock1H;
                if (clock3H >= 24){
                        clock3H -= 24;
                        }
        printf("%2d, %2d, %2d \n", clock3H, clock3M, clock3S);
        }
//printf("oustide if statement\n");
}
i have that issue fixed now, but it skips over the last scanf where i want the user to enter + or -
 
Joined
Jan 20, 2002
Messages
433
Here's an article that may help with the scanf("%c"):

http://cboard.cprogramming.com/c-pr...raft-why-scanf-%c-c-doesnt-work-expected.html

The problem is that the newline is left in the buffer from the previous line; scanf sees that and uses that for the character. This only happens with "%c" format.

By the way, if this is a "real" program (something that you want to be reliable, want to give to other people, etc.), note that scanf is not a safe way to collect and process input. It can't handle errors and it can result in buffer overflow. You might find this Wikipedia article helpful: http://en.wikipedia.org/wiki/Scanf_format_string
 

SuitedMonkey

Thread Starter
Joined
Nov 10, 2012
Messages
51
thanks so much for your help guys (and girls if that's what you are!) i got it working perfectly, here's the complete program!!
Code:
#include <stdio.h>
void main(){
int clock1H; //hours for clock 1
int clock1M; //minute for clock 1
int clock1S; // seconds for clock 1
int clock2H; //hours for for clock 2
int clock2M; //minutes for clock 2
int clock2S; //seconds for clock 2
int clock3H; //hours for clock 3
int clock3M; //minute for clock 3
int clock3S; // seconds for clock 3
char operator; //determines whether addition or subtracting will be used.
char cont;
do{
        printf("Please enter the time in 24 hour format for clock 1 in hour, minute, seconds \n");
        scanf("%d:%d:%d", &clock1H, &clock1M, &clock1S);

        printf("Please enter the time in 24 hour format for clock 2 in hour, minute, seconds \n");
        scanf("%d:%d:%d", &clock2H, &clock2M, &clock2S);

        printf("Please enter whether you want to + or - the clocks. \n");
        scanf(" %c", &operator);

        if (operator == '+'){
        printf("plus selected\n");
                clock3S = clock1S + clock2S;
                clock3M = clock1M + clock2M;
                clock3H = clock1H + clock2H;
                if (clock3S >= 60){
                        clock3M++;
                        clock3S -= 60;
                        }

                if (clock3M >= 60){
                        clock3H++;
                        clock3M -= 60;
                        }

                if (clock3H >= 24){
                        clock3H -= 24;
                        }
        printf("%d:%d:%d \n", clock3H, clock3M, clock3S);
        }
        else if (operator == '-'){
                 printf("minus selected\n");
                clock3S = clock1S - clock2S;
                clock3M = clock1M - clock2M;
                clock3H = clock1H - clock2H;
          if (clock3S < 0){
                        clock3M--;
                        clock3S += 60;
                        }
          if (clock3M < 0){
                        clock3H--;
                        clock3M += 60;
                        }

          if (clock3H < 0){
                        clock3H += 24;
                        }

                 printf("%d:%d:%d \n", clock3H, clock3M, clock3S);
        }
        printf("would you like to repeat with two new clocks? Y/N \n");
        scanf(" %c", &cont);

        }while(cont == 'Y' || cont == 'y');
}
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top