Solve Basic C++ Coding Challenges with Easy Steps

  • Context: Comp Sci 
  • Thread starter Thread starter SMA_01
  • Start date Start date
  • Tags Tags
    C++ Coding
Click For Summary

Discussion Overview

The discussion revolves around a basic C++ coding challenge that involves reading a name and age from user input and printing a formatted message. Participants are exploring issues related to code syntax and structure, including variable declaration and the use of standard input/output functions.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a code snippet intended to read a name and age but encounters issues, specifically with syntax errors related to the use of "count" instead of "cout".
  • Another participant points out the absence of a main function and the need for variable declarations, suggesting these are critical for the code to compile.
  • A third participant advises including "using namespace std;" to avoid using the std:: prefix for standard functions.
  • One participant argues that the assignment is ambiguous and suggests that the provided code technically performs the intended function, questioning whether the problem lies in the code itself or in the compilation process.

Areas of Agreement / Disagreement

Participants express differing views on whether the original code is fundamentally correct or if the issues stem from other factors such as missing declarations and function definitions. There is no consensus on the root cause of the problem.

Contextual Notes

Limitations include assumptions about prior variable declarations and the context of the assignment, which may not be fully articulated in the initial post.

SMA_01
Messages
215
Reaction score
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;
count << "The age of" << name << "is" << age << ".";

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

Thanks.
 
Physics news on Phys.org


SMA_01 said:
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;
count << "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 count.
 


Also you'll need to add:
using namespace std;
If you want to use count and cin without std:: prefix.
 


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.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 17 ·
Replies
17
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K