Thread: Beginner Help Please?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    16

    Beginner Help Please?

    I need help on how to combine input. For example, I declare the variables a and b

    int a,b;

    Next I plan on taking the input. If I use:

    scanf("%d%d", &a, &b);

    it will take input "a" on one line, and then input "b" on another. I want them to appear as a whole

    Instead of it taking input as:

    22
    11

    I want it taking it in as

    2211

    I need to do display it this way according to the instructions of my program. How can do I this?

    Sorry, if the answer is really easy, I'm a beginner.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Are your numbers strictly 2 digits each?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    Yes, it is supposed to be input for a 24 hour clock

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You can use "%2d%2d".

    Scanf - C++ Reference
    Devoted my life to programming...

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Zexious View Post
    Yes, it is supposed to be input for a 24 hour clock
    Oh? Why not then
    Code:
    if ( scanf( "%d:%d", &hrs, &mins) == 2 ) {
       /* make sure it's a real time value here */
    }
    In general, any character stands for itself in a format string, unless it's preceded by a % which implies a conversion of some sort.

    If you really insist on something like 2211 being valid, string manipulation, in addition to something like strtol, is probably best.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    OHHH I knew it was something simple. Thanks guys!!

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    @Whiteflags

    This is incredibly picky, but sometimes it's better to do something like this:

    Code:
    if ( scanf( "%d:%d", &hrs, &mins) != 2 ) {   
       /* error handling */ 
    }
    if ( hrs > 24 || mins > 60) {
       /* error handling */ 
    }
    
    /* your code */
    


    So your code doesn't end up looking like this:

    Code:
    if ( scanf( "%d:%d", &hrs, &mins) == 2 ) {    
       if ( hrs > 24 || mins > 60) {
          /* your code, let's hope it doesn't have many more indents */
       } 
    }


    Again, not really an issue, but throwing it out there just in case.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    Ok this is what I came up with so far using the knowledge I know. The program should operate like this:

    Sample Input

    1300

    Sample Output

    1300 in Ottawa
    1000 in Victoria
    1100 in Edmonton
    1200 in Winnipeg
    1300 in Toronto
    1400 in Halifax
    1430 in St. John’s

    However my code seems to not display some numbers at the end of the output. Can anyone help please?

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char *argv[])
    {
      int hrs,min;
      int vic,edm,win,hal,stj;
      
     scanf("%2d%2d", &hrs, &min);
     if ( hrs <= 23 && min <= 59 ) {     
     
     vic = hrs - 3;                   
     edm = hrs - 2;
     win = hrs - 1;
     hal = hrs + 1;
     stj = hrs + 1, min + 30;
         
          printf("The time in Ottawa is %d%d\n", hrs, min);
          printf("The time in Victoria is %d%d\n", vic, min);
          printf("The time in Edmonton is %d%d\n", edm, min);
          printf("The time in Winnipeg is %d%d\n", win, min);
          printf("The time in Toronto is %d%d\n", hrs, min);
          printf("The time in Halifax is %d%d\n", hal, min);
          printf("The time in St.John's is %d%d\n", stj);
    }
      
      system("PAUSE");    
      return 0;
    }
    The program is not complete yet, but I need to match the sample output before moving on

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Are you sure lines #17 and #25 do what you want?
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    I want to increase the time entered by 1 hour and 30 min and store it in stj. I guess I'm doing it wrong since it doesn't work

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Doesn't your compiler give you warnings about unused result and too few arguments passed to printf?
    Devoted my life to programming...

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    Well I guess it didn't for this case. I'm using Dev-C++

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You need to pass one more variable to the last printf. Declare another variable, replace the comma on line #17 with a semicolon, and assign "min + 30" to you new variable. Then add its name at the end of printf on line #25.

    EDIT: You may want to check if the different hours go below zero or if the minutes go above or are equal to 60, in which cases you'll have to correct them.
    Devoted my life to programming...

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by memcpy View Post
    Again, not really an issue, but throwing it out there just in case.
    Fair enough, but my comment didn't really say anything that you implied. I said to make sure the input was a real time value after the scanf returned. If people do get the impression to go on with the rest of the program under the scanf call from my snippet, well, you steered them clear....

  15. #15
    Registered User
    Join Date
    Feb 2012
    Posts
    16
    Thanks a lot!!! but I don't understand why my output is displaying for ex:

    The time in Halifax is 140

    instead of

    The time in Halifax is 1400

    EDIT: No I'm sure I set the appropriate conditions. If your're interested, you can read the problem here:

    Problem J3
    http://cemc.math.uwaterloo.ca/contes...1/juniorEn.pdf
    Last edited by Zexious; 02-12-2012 at 10:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner
    By bigjoe11a in forum C++ Programming
    Replies: 5
    Last Post: 01-02-2008, 10:50 AM
  2. Very much a beginner C ++
    By frontmaninback in forum C++ Programming
    Replies: 7
    Last Post: 08-03-2007, 04:06 PM
  3. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  4. beginner ?
    By braincrash in forum C Programming
    Replies: 2
    Last Post: 02-18-2003, 03:33 AM

Tags for this Thread