Basic C++ Coding?

  • Comp Sci
  • Thread starter SMA_01
  • Start date
  • #1
SMA_01
218
0
Basic C++ Coding???

Assume that name and age have been declared suitably for storing names (like "Abdullah", "Alexandra" and "Zoe") and ages respectively. Write some code that reads in a name and an age and then prints the message "The age of NAME is AGE." where NAME and AGE are replaced by the values read in for the variables name and age.
For example, if your code read in "Rohit" and 70 then it would print out "The age of Rohit is 70.".

Here is what I put in:

cin >> name >>age;
cout << "The age of" << name << "is" << age << ".";

I don't understand what I'm doing wrong.
Any help is appreciated.

Thanks.
 

Answers and Replies

  • #2
36,222
8,207


Assume that name and age have been declared suitably for storing names (like "Abdullah", "Alexandra" and "Zoe") and ages respectively. Write some code that reads in a name and an age and then prints the message "The age of NAME is AGE." where NAME and AGE are replaced by the values read in for the variables name and age.
For example, if your code read in "Rohit" and 70 then it would print out "The age of Rohit is 70.".

Here is what I put in:

cin >> name >>age;
cout << "The age of" << name << "is" << age << ".";

I don't understand what I'm doing wrong.
Any help is appreciated.

Thanks.
Two obvious problems are that you don't have a function named main, and you haven't declared the variables you're using.

You also need to have a #include directive so that you can use cin and cout.
 
  • #3
DIOGYK
4
0


Also you'll need to add:
using namespace std;
If you want to use cout and cin without std:: prefix.
 
  • #4
Borek
Mentor
29,003
3,669


I would say assignment is ambiguous. "Some code" that you wrote is technically doing what it should do, especially if we assume variables were declared earlier (as is suggested). I suppose you tried to compile the code and it didn't compile, so you assumed it is wrong - if so, look at the answers already given, but the problem doesn't lie in your two lines of code.
 

Suggested for: Basic C++ Coding?

  • Last Post
Replies
14
Views
1K
  • Last Post
Replies
2
Views
587
  • Last Post
Replies
6
Views
3K
  • Last Post
Replies
9
Views
1K
  • Last Post
Replies
5
Views
3K
  • Last Post
Replies
12
Views
815
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
7
Views
2K
  • Last Post
Replies
11
Views
2K
Replies
0
Views
2K
Top