Solve Basic C++ Coding Challenges with Easy Steps

  • Comp Sci
  • Thread starter SMA_01
  • Start date
  • Tags
    C++ Coding
In summary, your code seems to work, but you might want to look at some of the other answers to see where you might be going wrong.
  • #1
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.
 
Physics news on Phys.org
  • #2


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;
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


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


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.
 
  • #5


I am not an expert in C++ coding, but here is my understanding and suggestion for your code:

1. You have declared the variables "name" and "age" correctly, so that's great.

2. When using cin to read in user input, it is important to specify the type of data you are expecting. In this case, you are expecting a string for the name and an integer for the age. So your code should be:

cin >> name >> age;

3. The issue with your code is in the cout statement. You are missing spaces between the words and variables. The correct code should be:

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

4. Remember to include the quotation marks around the words you want to print out as they are not variables.

I hope this helps clarify the issue with your code. Keep practicing and you will become more familiar with C++ coding challenges. Good luck!
 

1. What is C++ coding?

C++ is a high-level programming language that is widely used in software development. It is an object-oriented language that allows programmers to create efficient and fast programs.

2. What are basic C++ coding challenges?

Basic C++ coding challenges are problems or tasks that require the use of C++ programming language to solve. They typically involve writing code to perform specific tasks, such as calculating a mathematical equation or sorting a list of numbers.

3. How can I solve basic C++ coding challenges?

To solve basic C++ coding challenges, you will need to have a good understanding of the C++ language and its syntax. It is also helpful to have a logical and analytical mindset, as well as the ability to break down a problem into smaller, more manageable steps. Practice and experimentation are also key to improving your skills in solving coding challenges.

4. What are some easy steps to solve C++ coding challenges?

Some easy steps to solve C++ coding challenges include understanding the problem, breaking it down into smaller sub-problems, writing pseudocode, coding the solution, testing and debugging, and finally optimizing the code for efficiency.

5. Are there any resources available to help me solve basic C++ coding challenges?

Yes, there are many online resources, such as coding websites and forums, that offer practice problems and solutions for basic C++ coding challenges. Additionally, there are books, tutorials, and online courses that can help you improve your C++ coding skills and solve more complex challenges.

Suggested for: Solve Basic C++ Coding Challenges with Easy Steps

Replies
2
Views
1K
Replies
1
Views
871
Replies
2
Views
2K
Replies
2
Views
826
Replies
8
Views
752
Back
Top