Very basic C++ question

Soldato
Joined
1 Jan 2007
Posts
3,186
Location
Exeter
Hi all,

I'm moving from Java to C++, and the minor differences between the two are really bugging me. I'm looking for code to read in an integer from a prompt, but for the prompt to have a string variable in it. For example, if I had a string variable 'Item' with a value "Cd-rom", I'd want the prompt to say "Enter the price of Cd-rom". So the code should look something like
Code:
ItemPrice = ReadIntPr("Enter the price of ," Item);
C++ doesn't like that (says the 'Item' is an extra parameter), and I'm probably missing something obvious, but I don't know what to google to find the answer.

Cheers,

MD
 
cout << "Enter the price of " << Item << endl;
cin >> ItemPrice;

that would display "Enter the price of " value of Item variable and then ask to prompt in ItemPrice
 
Last edited:
is the , in the wrong place in your code?

Code:
ItemPrice = ReadIntPr("Enter the price of ", Item);
EDIT: on second throughts you may need to alter it to

Code:
ItemPrice = ReadIntPr "Enter the price of ";
cin >> Item;
 
Last edited:
the stuff Leodido gave is pretty simple :)

I think as long as you've got the following before your int main() bit it should work fine

Code:
#include <iostream>
using namespace std;
Then inside your int main() function you can use cin and cout.

cout simply outputs to the command window

cin simply puts what you've typed in the command window into a variable or somethin

e.g.

Code:
cout << "enter a price" << endl; //outputs "enter a price" to the screen
cin >> Item; //after you type a price in, it gets stored in item
 
Last edited:
Back
Top Bottom