Thread: Jumping into c++ help

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    2

    Jumping into c++ help

    Hi guys I am completely new to c++ and i decided to work my way through - Jumping into c++. I am currently up to chapter 5 and have found myself stumped a couple of times along the way.

    My current predicament is practice problem 7 in chapter 5: Write a program that provides the option of tallying up the results of a poll with 3 possible values. The first input to the program is the poll question; the next three inputs are the possible answers. The first answer is indicated by 1, the second by 2, the third by 3. The answers are tallied until a 0 is entered. The program should then show the results of the poll—try making a bar graph that shows the results properly scaled to fit on your screen no matter how manyresults were entered.

    My code is as follows, thanks for your help in advance.

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        int answer;
        int a = 0;
        int b = 0;
        int c = 0;
    
    
        do
        {
        cout << "Who is the best?" << "\n" << "1. Zach" << "\n" << "2. Michelle" << "\n" << "3. Jack" << "\n";
        cin >> answer;
    
    
        if (answer == 1)
        {
            a++;
        }
    
    
        else if (answer == 2)
        {
            b++;
        }
    
    
        else if (answer == 3)
        {
            c++;
    
    
        }
    
    
        else if (answer == 0)
        {
            cout << "Zach = " << a << "\n" << "Michelle = " << b << "\n" << "Jack = " << c << "\n";
        }
    
    
        else
        {
        cout << "Who is the best?" << "\n" << "1. Zach" << "\n" << "2. Michelle" << "\n" << "3. Jack" << "\n";
        cin >> answer;
        }
        }
        while (answer < 0);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    first thing - your line 54 condition should be opposite. You want to continues looping while answer is >= 0

    You also should work on your indentation.


    And describe your problem, not only what you are trying to do - but also what results you get and how it is different from your target.
    Last edited by vart; 08-03-2013 at 10:13 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    2
    Oh, sweet. it doesnt break anymore and the tally continuously adds. Could you please tell me how i stop it from spamming my screen? i would like the question to come up on a fresh page each time an answer is entered. While still adding to the tally.

    I think perhaps im a little slower than even this book, finding it overwhelming :/

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. after 'jumping to c++'
    By nish1013 in forum C++ Programming
    Replies: 2
    Last Post: 07-09-2013, 08:26 AM
  2. Jumping into C++
    By Tamim Ad Dari in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2013, 08:29 PM
  3. Jumping into C++
    By Tamim Ad Dari in forum C++ Programming
    Replies: 9
    Last Post: 01-11-2013, 09:45 AM
  4. Jumping to OOP in C++ from C
    By meadhikari in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2010, 05:26 AM
  5. Jumping in the code...
    By AdampqmabA in forum C++ Programming
    Replies: 24
    Last Post: 10-06-2008, 05:34 PM