Thread: Brand new to c++ and need help with a school task

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    2

    Brand new to c++ and need help with a school task

    Hello, I have to ask a user to enter integers or to enter q to quit. I have written a program that finally allows the user to enter q without the program entering an infinite loop. Now I am unable to enter the number 0 without triggering my break.

    How can I allow the user to enter 0 so that it applies towards total and q to quit the program?

    This is for a midterm and has to remain simple.

    Thank you

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        int integer = 0;
        int count = 0;
        int total = 0;
        int odd = 0;
    
        do
        {
            cout << "Enter integer (q to quit): ";
            cin >> integer;
            if (integer == false)
            {
                break;
            }
            if (integer % 2 == 1)
            {
              cout << "we have an odd" << "\n";
              odd++;
              total += integer;
            }
            
        } while (integer == true || integer >= 0 || integer <= 0);
        
        cout << "Odd = " << odd << endl;
        cout << "Total = " << total << endl;
        cout << "Goodbye!";
        
       
    return 0;
    }
    Attached Files Attached Files
    Last edited by Salem; 03-03-2020 at 10:29 PM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,994
    There is no "this isn't an integer" test in the manner you wrote it.
    Code:
            cout << "Enter integer (q to quit): ";
            cin >> integer;
            if ( !cin )
            {
                break;
            }
    Typing 'q' when expecting an int puts the stream into an error state, and this is what you need to detect.


    But if you wanted to input anything else after your while loop, you would also need to call cin.clear() as well.

    Of course, it was cross-posted
    Brand new to c++ and need help with a sc - C++ Forum

    Read this!!
    How To Ask Questions The Smart Way
    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
    Mar 2020
    Posts
    2
    Thank you, I very much appreciate your help. It was exactly what I was looking for, but I did not know how to properly ask the question. I tried my best to search for the answer, but was confused by all answers I found. I did become a bit worried that I would not figure this out and that is why I created accounts and made my first posts in two different forums. I apologize for doing that. I am now aware of cross-posting and will not do it again. I am only a month in to coding and I hope this mistake does not prevent me from asking questions here again.

    Sorry to have wasted anyone's time.

    Thank you again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-14-2019, 04:49 AM
  2. Brand new to programming
    By Dogmasur in forum C Programming
    Replies: 12
    Last Post: 08-02-2008, 01:27 AM
  3. Replies: 2
    Last Post: 12-31-2007, 11:40 AM
  4. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 10:08 AM
  5. I am BRAND new to this, so please go easy on me
    By christianne in forum C Programming
    Replies: 12
    Last Post: 03-30-2006, 07:58 AM

Tags for this Thread