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.

Recommended Answers

All 4 Replies

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

while (cin>> product)

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

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.