175k views
2 votes
This is C++ Assignment

Assignment 5 (10 points): Looping

Your goal is to take in data from the user, write equations, and use loops for validation and output.

Ask the user for a number between 1-10. Validate if this number is correct using a loop. Keep asking for a number until it is a valid number (one between 1-10).

Use the number entered to use in a "for loop," a "while loop," and a "do while loop."

The loops will output your name the number of times entered by the user.

Example output:

Enter a number (between 1 and 10): 3

For Loop:

name. name. name

While Loop:

name. name name

do While Loop:

name name name

User Mpak
by
7.2k points

1 Answer

7 votes

Final answer:

To solve this C++ assignment, you need to ask the user for a number between 1 and 10 using a loop, validate the number, and use it in a for loop, while loop, and do-while loop to output your name the number of times entered. Here's an example code that accomplishes this task.

Step-by-step explanation:

To solve this C++ assignment, you need to ask the user for a number between 1 and 10 using a loop. Validate if the number is correct and keep asking until a valid number is entered. Then, you can use the entered number in a for loop, a while loop, and a do-while loop to output your name the number of times entered by the user.

Here's the code example:

#include <iostream>
#include <string>
int main() {
int number;
std::string name = "name";
do {
std::cout << "Enter a number (between 1 and 10): ";
std::cin >> number;
} while (number < 1 || number > 10);
std::cout << "For Loop:";
for (int i = 0; i < number; i++) {
std::cout << name << ". ";
}
std::cout << "\\While Loop:";
int i = 1;
while (i <= number) {
std::cout << name << ". ";
i++;
}
std::cout << "\\Do While Loop:";
int j = 1;
do {
std::cout << name << " ";
j++;
} while (j <= number);
return 0;
}

User Petar Sabev
by
8.8k points