Question:
How can I refer to any letter or word variable in C++?
James
2012-01-02 11:57:56 UTC
I am trying to make a C++ program that can store info about someone an I need to refer to the cin >> input. Here is what it looks like so far (wherever there is a ?, that's where I don't know what to put):

#include
#include "stdlib.h"

using namespace std;

int main()
{ string name, animal;
int age, n;

cout << "I will now quiz you..." << endl;
cout << endl;
cout << "What is your name?";
cin >> name;

name = ? (I want to refer to the cin >> input);

cout << "How old are you?";
cin >> n;

age = n;

cout << "Lastly, what is your favorite animal?";
cin >> animal;

animal = ? ( I want to refer to the cin >> input);

cout << "Your name is " << name << ", you are " << age << " years old, and your favorite animal is a " << animal << "." << endl;

system ("pause");
return 0;
}
Three answers:
?
2012-01-02 12:03:23 UTC
For the first output of the name, all you need to do is "cout << name << endl;"...when you read information via cin that information is stored in the variable you read it in to. When you do "cin >> name;" the whole "variable = input" thing is done automatically for you, so there's no need to go reassigning variables after cin statements.
randomness123
2012-01-02 12:56:58 UTC
Wow this was written so poorly... just saying. Why exactly are you making age = n it is unnecessary.

The cin >> name and cin >> animal is already stored in the variables so there's no need to keep on reassigning the variables it will make your work sloppy and cause errors. Oh and a pro tip don't use system("pause") it will waste CPU and cause problems when you make much bigger problems just use cin.get() or if you're using code::blocks don't use anything at all because it does it for you.

So anyway this should be a very very easy program to make but by the looks of it you are a beginner and if you are then good job making mistakes(seriously) you'll learn from them and break bad habits

Well happy coding
?
2012-01-02 12:04:44 UTC
you do need to do that... you got the name already store why would you wanna refer that to another variable for, get rid of that it makes no sense.



age = n; <---- Dont need this either, age is already stored in age.



Dont use system pause. and learn how to indent properly.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...