I'm currently in a C++ class and I'm not sure what to do in this particular assignment. The book isn't very helpful and I feel like I need assistance. Here is the assignment:
Write a C++ program to print a number from 1 to a user entered number. Only accept numbers from 1 to 100; Example: Enter the number: 15 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15.
At first, I thought, maybe it was a guessing game, but it asks to PRINT all the numbers from 1-100. I know I need to use a for loop. Here is what I have so far:
#include <iostream>
#include <cstdlib>
#include <ctime>
{
int num = 15;
for (int i = 0; i < 100; i++)
}
system ("Pause")
You probably DON'T need "cstdlib".
Instead, consider using "cin" and "cout":
http://www.cplusplus.com/doc/tutorial/basic_io/
int age;
cin >> age;
...
cout << "I am " << age << " years old.";
std::cin.ignore();
: Is there a decent wait function in C++?
main()
somewhere, if you don't already have one. Your example has no function, and won't compile as-is.From what I understand, you need to print numbers from the 1 to the user-inputed number (but only accept numbers from 1 to 100). Then the following should work:
// This will import the libraries we need for input and output
#include <iostream>
int main(int argc, char * argv[]) {
// This will get the number from the user
int num;
std::cin >> num;
// We check if it's in the right range; if so, print the numbers we want
if(0 < num && num <= 100) {
for(int i = 1; i <= num; i++) {
std::cout << i << std::endl;
}
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.