Problems building a "command-line" calculator

This is a discussion on Problems building a "command-line" calculator within the C Programming forums, part of the General Programming Boards category; I'm having an error as you can see in the image I attatched. I also attached the only c file ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    24

    Exclamation Problems building a "command-line" calculator

    I'm having an error as you can see in the image I attatched. I also attached the only c file of this program in which the error resides.
    Attached Images Attached Images  
    Attached Files Attached Files
    • File Type: c main.c (660 Bytes, 56 views)

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,487
    How many times do you think this for loop will run.
    Assume i does not equal a. Or, let a be equal to 4.
    Hint: Do you know what "==" means?
    Code:
    for(i=0; i == a; i++)
    If hint is not enough read http://www.cprogramming.com/tutorial/c/lesson3.html

    Tim S.
    Last edited by stahta01; 10-30-2011 at 05:14 PM.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Stop and think what would happen if someone put in -1 ....

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Oh thank you, I discovered my error. I should have put:
    Code:
    for(i=0; i < a; i++)
    		{
    			printf("Your Number(s):  ");
    			scanf("%d", &l);
    			int b = l + b;
    		}
    Last edited by mmario10; 10-31-2011 at 03:59 AM.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    I solved that error, but when i reach the answer, it still gives me 0!!

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    Quote Originally Posted by mmario10 View Post
    Code:
    int b = l + b;
    Have a look at this line. It creates a new variable named b, which happens to be initialized using the value of b at another scope. The one created inside the loop will not exist outside it.

    Using a variable named 'l' is also an extremely bad idea, only justified by laziness. It is quite difficult to distinguish an 'l' (lowercase L) from a '|' (bitwise or). It can, depending on screen characteristics, be difficult to distinguish from other characters as well.
    Last edited by grumpy; 10-31-2011 at 04:20 AM.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    24
    Fixed the error, thanks. And changed the 'l' variable to 'r'. Intead of doing:
    Code:
     int b = r + b;
    I did:
    Code:
     b = r + b;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-18-2011, 11:51 AM
  2. Print name "x" times using command line arguments
    By zekiegypt in forum C Programming
    Replies: 3
    Last Post: 04-20-2011, 11:32 AM
  3. Replies: 3
    Last Post: 06-01-2004, 04:29 PM
  4. need help with C and a "command line?"
    By esjr in forum C Programming
    Replies: 1
    Last Post: 02-02-2003, 06:51 PM
  5. command line "shell"
    By mart_man00 in forum C Programming
    Replies: 3
    Last Post: 09-29-2002, 01:29 AM

Tags for this Thread


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