0

This is my 1st time working with C++ and im trying to learn the lanuage. I am writing a program that figures out the total sales data for five products. The code for the program is:

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int product;
    int sold;
    double price = 0.00;
    double total = 0.00;

    cout <<"Please enter a product number.(Press ctrl+z to end)" <<endl;
    cin >> product;

while ((product = cin.get()) != EOF)
{
switch (product) {

    case '1':
        cout<< "How many were sold?"<<endl;
        cin >> sold;
        price = 2.98;
        total = total + (price * sold);
        cout << "Enter a product number. (Press ctrl+z to end)" << endl;
        break;

    case '2':
        cout<< "How many were sold?"<<endl;
        cin >> sold;
        price = 4.50;
        total = total + (price * sold);
        cout << "Enter a product number. (Press ctrl+z to end)" << endl;
        break;

    case '3':
        cout<< "How many were sold?"<<endl;
        cin >> sold;
        price = 9.98;
        total = total + (price * sold);
        cout << "Enter a product number. (Press ctrl+z to end)" << endl;
        break;

    case '4':
        cout<< "How many were sold?"<<endl;
        cin >> sold;
        price = 4.49;
        total = total + (price * sold);
        cout << "Enter a product number. (Press ctrl+z to end)" << endl;
        break;

    case '5':
        cout<< "How many were sold?"<<endl;
        cin >> sold;
        price = 6.87;
        total = total +(price * sold);
        cout << "Enter a product number. (Press ctrl+z to end)" << endl;
        break;

    case '\n':
    case '\t':
    case ' ':
        break;

    default:
        cout << "Not a valid product number.  Please enter another number.";
            break;
    }

}

cout << "The total value of merchandise sold last week was: " << fixed <<setprecision (2) << total << endl;

return 0;

}

The program works but to get it started I have to enter a number twice. Is there something in my code causing it to do that or is just a C++ quirk. I would greatly appreicate any help that is offered.

Edited by pyTony: fixed formating

2
Contributors
4
Replies
5
Views
12 Years
Discussion Span
Last Post by Narue
0

>while ((product = cin.get()) != EOF)
Change this to:

while (cin>> product)

And remove the first request for input outside of the loop.

0

When i do that i no longer wants to accept any input. It keeps on going to the default case and not processing anything.

0

Fixed the problem. I just need to remove the 1st input request outside of the loop.

0

And remove the first request for input outside of the loop.

It's a good thing that you read my post, otherwise you might have problems.

When i do that i no longer wants to accept any input.

Uh oh, looks like you're having problems. Are you sure you read my post in it's entirety?

Fixed the problem. I just need to remove the 1st input request outside of the loop.

Guess not. I'm beginning to wonder if you people even bother to read my posts.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.