Thread: Do...While Correction Needed

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    Do...While Correction Needed

    I just recently started learning this stuff so I am sure I am doing something simple but I don't really notice a difference from my book examples. I am trying to have the program loop back to ask the original question "Enter and operation: + - * / (or enter X to exit): " but instead it loops through the if statement. So it keeps asking me for a number instead of a new operator and THEN a new number, but it's not actually performing the math either. This program is supposed to continuously perform math to a number until you press "X" to stop.

    Code:
    do 
    		   {
    			   cout << "Enter and operation: + - * / (or enter X to exit): "; cin >> oper;
    			   if (oper = '+')
    			   {
    				   cout << "Enter a number: "; cin >> num;
    				   tot = tot + num;
    				   cout << "Current total is " << tot << endl;
    			   }
    			   if (oper = '-')
    			   {
    				   cout << "Enter a number: "; cin >> num;
    				   tot = tot - num;
    				   cout << "Current total is " << tot << endl;
    			   }
    			   if (oper = '*')
    			   {
    				   cout << "Enter a number: "; cin >> num;
    				   tot = tot * num;
    				   cout << "Current total is " << tot << endl;
    			   }
    			   if (oper = '/')
    			   {
    				   cout << "Enter a number: "; cin >> num;
    				   tot = tot / num;
    				   cout << "Current total is " << tot << endl;
    			   }
    			   else cout << "Enter a valid number." << endl;
    		   }
    		   while (oper != 'X' || oper != 'x');

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    For the same reason as in a previous post of yours
    Double returns 1.#INF
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    I am not getting that error, or any compiling error for that matter, though. It just asks for the operator the first time then keeps asking for a number (and not actually doing the math either).

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I think what's happening is that when you enter your number, it's leaving your carriage return character in the buffer. you need to call cin.ignore() immediately after your call to cin>> to remove it. without removing this character, it will just be read in by the cin>> that accepts the type of operation, and since a carriage return is not in the list of accepted inputs it just loops back again. also, you might consider putting an if() after the operation selection, so that if the user wants to exit before performing any operations at all, he/she can.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    So issues like
    if (oper = '/')

    are NOT the problem?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Salem, yes I just figured out that I needed the double =. Otherwise, I had not been getting an area and I really thought I had the == originally but probably was not using single quote with the char at the time so it was a double whammy and I only fixed one issue.

    My next issue is that my else statement is always being called even though I am always using one of the valid operators that calls an IF statement. I had a feeling this would be the next problem I encountered at least, so I guess I am learning a little as I go.

    Elkvis I will look into what you were saying too, especially about allowing people to exit before any operation. I think the other end was solved by the use of == instead of =.

    Edit: I must also have written the while incorrectly as X and x are not aborting the loop. Going to look into that one awhile
    Last edited by jae5086; 10-21-2010 at 10:54 AM.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    To follow up, I figured out the reason why my else was popping up all the time, I used or (||) instead of and (&&) in my condition. Silly mistake.

    So that leaves my (hopefully) final issue, the loop doesn't terminate when I use X or x.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Ok I figured this one out too. Using || when I should have been using &&. Let's hear it for logic!

    /thread closed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. Programmer Needed for Software Patch
    By dhigginbotham in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 07-26-2007, 04:06 PM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. Commands needed
    By bookworm in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-16-2003, 04:50 PM