Program Problem

This is a discussion on Program Problem within the C Programming forums, part of the General Programming Boards category; My mom got me a good book on C I've been learning alot! And I wanted to make this calculor ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    41

    Unhappy Program Problem

    My mom got me a good book on C I've been learning alot! And I wanted to make this calculor with a menu, and you type 1 to add some numbers, then it asks you for how many numbers you want to add, then you type in the value.

    The problem is that some times when it's asking you what the numbers you want to add(not to be confued with HOW MANY numbers you want to add)you type in the value and press enter and it add another line and you have to add another number to goto the next one I've got a screen shot to show you:

    http://mksoftg.tripod.com/CS.mks


    (the screen shot is a bit messed up but you can see the problem area)

    and when it add it all up it prints a big number!

    heres the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
      int choice, numAdd, temp, numTotal = 0;
        
      printf("Welcome to Karl's easy math Calculator!\n\n");
      
      do
      {
       printf("1: Add\n2: Subtact\n3: drivde\n4: Exit\n\nWhat you want to do?\n");
       scanf(" %d",&choice); 
       /******************************************/
       switch (choice)
       {
          case (1) : { printf("How many numbers are you going to add?\n");
                       scanf("%d", &numAdd);
                          /******************************************/
                          for(; numAdd > 0; numAdd--)
                          {
                             printf("Whats a number that you want to add?");
                             
                             scanf("%d ", &temp);
                             
                             numTotal += temp;
                             temp = 0;
                          }
                          /******************************************/
                       printf("Thats up to %d\n\n", &numTotal);
                       break;
                       }
                       
          default : { printf("Unknown Number, plaese type again"); }  
       }
       /******************************************/
      } while ((choice < 1) || (choice > 4));
      
      
      system("PAUSE");	
      return 0;
    }
    Note: I'm useing Dev-Cpp so I don't know if this code will work on all compilers...

    as you can see, it's not done yet, I want to get pass this first.

    plx reply, thx

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    just a couple things outta place.... I *think*

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      int choice, numAdd, temp, numTotal = 0;
        
      printf("Welcome to Karl's easy math Calculator!\n\n");
      
      do
      {
       printf("1: Add\n2: Subtact\n3: drivde\n4: Exit\n\nWhat you want to do?\n");
       scanf("%d%*c",&choice); 
       /******************************************/
       switch (choice)
       {
          case 1 : { printf("How many numbers are you going to add?\n");
                     scanf("%d", &numAdd);
                          /******************************************/
                          for(; numAdd > 0; numAdd--)
                          {
                             printf("Whats a number that you want to add?");
                             
                             scanf("%d%*c", &temp);
                             
                             numTotal += temp;
                             temp = 0;
                          }
                          /******************************************/
                       printf("Thats up to %d\n\n", numTotal);
                       
                       } 
                       
                       break;
                       
          default : { printf("Unknown Number, plaese type again"); }   break;
       }
       /******************************************/
      } while ((choice < 1) || (choice > 4));
      
      
      system("PAUSE");	
      return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    You need to zero your total accumulator 'numTotal' at the top of you do loop, otherwise your second run will be added to your first total.

    The change ronin mentions:
    scanf("%d%*c", &temp);
    was not necessary with my compiler, but might be with yours. I rarely used scanf because it can be a little ideosyncratic depending on the compiler. Try printing the value read immediately after the scanf to check it.

    Alternatively, uset gets() or fgets() to enter a string which will read off the CRLF too and use the function atoi() to convert it to a number.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    41

    Talking YAY! It workz!

    Thx, now it works!

    WaltP, could I do this:

    for(numTotal = 0; numAdd > 0; numAdd--)

    and ronin why do I need to do scanf("%d%*c", &temp); and all those's %'s?
    Last edited by Budgiekarl; 05-06-2003 at 07:16 AM.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: YAY! It workz!

    >>could I do this:
    >>for(numTotal = 0; numAdd > 0; numAdd--)
    Yes, if you want to.

    >>why do I need to do scanf("%d%*c", &temp); and all those's %'s?
    It's a poor mans version of obtaining a number from the user.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    Re: Re: YAY! It workz!

    Originally posted by Hammer
    >>why do I need to do scanf("%d%*c", &temp); and all those's %'s?
    It's a poor mans version of
    Hmph that hurt.

    yea, it's just a crude way to eat the '\n' that was left in the input buffer after getting the int data type.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Nothing personal, ronin

    But if you are going to use scanf(), at least ensure you check it's return code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 10:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 01:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21