Do..While Loops

This is a discussion on Do..While Loops within the C++ Programming forums, part of the General Programming Boards category; New Project... need help please: Attached is the instructions. Here is what I have so far and having trouble: Code: ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    36

    Do..While Loops

    New Project... need help please:

    Attached is the instructions.

    Here is what I have so far and having trouble:

    Code:
    #include <iostream>
    using namespace std;
    
    //Write a simple calculator program. Ask the user if s/he wants to add ('+') subtract ('-'), multiply ('*') or divide ('/') 
    //and then ask for the number. Perform the operation with the given number. Start the "display" at zero. Stop when 
    //the user enters 'X' (for exit). Use a Do..While loop and "if" statements. 
    int main()
    {
    	double total = 0, n = 0;
    	char operation =  ' ';
    	
    	cout << "Current total is 0" << endl;
    
    		//Asking for user input 
    			do
    			{
    			cout << "Enter an operation: + - * / (or enter X to exit): ";
    			cin >> operation;
    			if (operation == 'X')
    				{
    					break;
    				}
    				 
    				if ((operation != '+') && (operation != '-') && (operation != '*') && (operation != '/'))
    						do 
    						{
    							cout << "Enter an operation: + - * / (or enter X to exit): ";
    							cin >> operation;
    						}
    						while ((operation != '+') && (operation != '-') && (operation != '*') && (operation != '/'));
    				
    				cout << "Enter a number. " ;
    				cin >> n;
    				if (operation == '/') 
    					
    					do
    					{
    						cout << "Can not divide by zero!" << endl;
    						cout << "Current total is " << total << endl;
    					}	
    					while (n = 0);
    
    				 else cout << "Current total is ";
    					
    						if (operation == '+') 
    							total = (total + n);
    				 
    							if (operation == '-') 
    								total = (total - n);
    
    								if (operation == '*') 
    									total = (total * n);
    	
    									if (operation == '/') 
    										total = (total / n);
    									
    											
    					cout << total << endl;			
    			
    			}
    			while (operation != 'X');	
    			 
    }
    Attached Images Attached Images

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Wow...nice indentation!

    What's the problem?

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    36
    I have the program initiating "total" twice when dividing by zero and the 2nd time is nonsense. Also I am getting a "Can not divide by zero" comment for non zero divisors (example 23/3?)


    aghhh,

    I am missing something.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Titanguy View Post
    Code:
    if (operation == '/') 
    					
    do
    {
    cout << "Can not divide by zero!" << end
    cout << "Current total is " << total << endl;
    }	
    while (n = 0);
    I would change the above to:

    Code:
    if (operation == '/' && total == 0) 
    					
    					
    {
    cout << "Can not divide by zero!" << endl;
    cout << "Current total is " << total << endl;
    }

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Code:
    while (n = 0);
    = is for assignment, == is for comparison

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Start putting braces around your ifs. Then stop adding additional tabs for every if. For each brace, +1 tab ONLY.

    warning C6282: Incorrect operator: assignment of constant in Boolean context. Consider using '==' instead
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Indentation for if's:

    Code:
    if (conditional)
    {
        DoSomething();
        if (another_conditional)
       {
            DoSomethingElse();
       }
    }
    You don't have to provide braces if the conditional only leads to one statement but I find that always using braces for if's results in much cleaner code.

    If you are using MSVC you can usually rely on their default indentation to be correct. There are only two areas that really annoy me in MSVC:

    • It does not indent over once before writing the body of a class.
    • It indents braces inside switch statements.


    IE: MSVC's suggestion
    Code:
    class Example
    {
    public:
         Example();
         ...
    };
    
    switch (some_value)
    {
        case 0:
            {
                 DoSomething();
                 break;
            }
    }
    My preference:
    Code:
    class Example
    {
        public:
             Example();
             ...
    };
    
    switch (some_value)
    {
        case 0:
        {
             DoSomething();
             break;
        }
    }
    Other than those two areas I usually just allow the compiler to figure out the indentation. Of course it has become so automatic for me I'm not sure what the editor in the compiler actually suggests anymore.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    My preference:
    Code:
    class Example
    {
    public:
        Example();
        ...
    };
    
    switch (some_value)
    {
        case 0:
        {
             DoSomething();
             break;
        }
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do/While Loops
    By sinamor1210 in forum C Programming
    Replies: 6
    Last Post: 09-20-2009, 11:15 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  3. strings and loops...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-12-2005, 10:28 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM
  5. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM

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