-1

I want to promt for a string variable (name of item) and then promt for a double variable (cost). I want this to be done 5 times so each time it loops the vales are stored as a different pair of variable.

need to have user input an item and then its price so i can calc a bill. not sure if I can crate a loop for this or i need to keep a running count somehow

int main()
{

    int i;
    string  Item_1,Item_2,Item_3,Item_4,Item_5;

    double Price_1,Price_2,Price_3,Price_4,Price_5 ;

    while (i<6)
    {

    cout<<"Please enter item"<<endl;
    cin>> Item_1>>Item_2>>Item_3>>Item_4>>Item_5>>endl; 

    cout<<"Please enter cost of   " >> Item_1>>Item_2>>Item_3>>Item_4>>Item_5;
    cin>>Price_1>>Price_2>>Price_3>>Price_4>>Price_5;

    i=i++
    }

    return 0;

}

Code doesn't compile but i expect it to ask for my in put for the 5 variables 5 times

YSC
  • 38,212
  • 9
  • 96
  • 149
  • 1
    Hi. You are saying the code doesn't compile -- what errors are you getting? Right off the bat, `<>` isn't an operator, and you can't use `>>` with `std::cout`. To answer your initial question, yes, loops can be used to collect user input. – AlexG Mar 28 '19 at 14:23
  • 4
    ...and `i=i++` is both undefined behavior and lacking a semicolon. Just write `i++;`. – Max Langhof Mar 28 '19 at 14:23
  • You want an array or a `std::vector`. (And a structure to store your item information in.) – molbdnilo Mar 28 '19 at 14:26
  • Right now you are executing everything inside the loop 6 times (for i = 0, 1, 2, 3, 4, and 5). You are asking the user 6 times for 5 items. You want to _either_ ask the user _once_ for _all five items_, or you want to loop over it. Honestly though, this is a case of not understanding the most basic elements of the language. You will need a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or your learning efforts won't go anywhere. – Max Langhof Mar 28 '19 at 14:26
  • Remember that uninitialized local variables will have an *indeterminate* (and seemingly random or "garbage") value. With that knowledge please do some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) of your code (especially the loop condition). – Some programmer dude Mar 28 '19 at 14:27
  • You can't use `>>` with an output stream. Too much copy and paste? – molbdnilo Mar 28 '19 at 14:31
  • i is not initialized either – Alexey Godin Mar 28 '19 at 15:50

2 Answers2

1

Here is a solution with arrays and a for loop.

You can try it in CPP Shell.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string  Item[5];

    double Price[5] ;

    for(int i = 0; i < 5; i++)
    {

        cout<<"Please enter item"<<endl;
        cin>> Item[i];

        cout<<"Please enter cost of "  << Item[i] << ":" << endl;
        cin>>Price[i];
    }


    cout << "Items: ";
    for(int i = 0; i < 5; i++)
    {
        cout << Item[i] << " ";
    }

    cout << endl << "Prices: ";
    for(int i = 0; i < 5; i++)
    {
        cout << Price[i] << " ";
    }

    return 0;
}
MrMaavin
  • 1,611
  • 2
  • 19
  • 30
0

Your code doesn't compile for many reasons!

Of course you can use a loop for your purpose. Consider the following code, which is not really good, but does the job:

#include <string>
#include <iostream>

using namespace std;

int main()
{

    int i = 1;
    string Item[5];

    double Price[5];

    while (i<6)
    {
        cout<<"Please enter item "<< i << ": ";

        cin>>Item[i-1]; 
        cout<<endl;

        cout <<"Please enter cost of "<< Item[i-1] << ": ";
        cin >> Price[i-1];

        i++; 
    };

    i = 1;
    while (i<6)
    {
        cout << "Item " << i << ": " << Item[i-1] << ", Price: " << Price[i-1] << endl;
        i++;
    };
    return 0;

}
mic
  • 235
  • 1
  • 12
  • is there a simple way to find the totals of the cost? or do i have to type out each variable and multiply by the qty? – J.Ramboi Mar 28 '19 at 15:22
  • @J.Ramboi Of course you can add all elements of Price[]: declare a variable "double TotalPrice;" and set it to TotalPrice = Price[1] + Price[2] + Price[3] + Price[4] + Price[5]; And of course you can use a loop for this. And of course you can multiply each price with a quantity. – mic Mar 28 '19 at 15:48
  • @J.Ramboi BTW... everyone is happy to help here. But all your questions are not only related to the very basics of C++, but also to the very basics of programming. Please practice some beginner tutorials on C++ and try to answer such things by yourself. Showing some effort is always appreciated and leads to positive reputation :-) – mic Mar 28 '19 at 15:51