1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: scanf usage

Discussion in 'Software Development' started by SuitedMonkey, Jan 30, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. SuitedMonkey

    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...
     
  2. jdean

    jdean

    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.
     
  3. SuitedMonkey

    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 -
     
  4. jdean

    jdean

    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
     
  5. SuitedMonkey

    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');
    }
    
     
  6. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1087609

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice