-4

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")
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Niki M
  • 31
  • 1
  • 5
  • You should read about `cin` – eavidan Sep 25 '16 at 17:54
  • 3
    NEVER use `system` unless you really know what you're doing, or your teacher is forcing you to use it for some dumb reason. – Carcigenicate Sep 25 '16 at 17:54
  • 2
    Unfortunately, budding C++ developers are sometimes forced to use technically-inferior operating systems where nonsense like `system("pause");` is required just to be able to read your program's output, otherwise it goes to that great bit bucket in the sky. – Sam Varshavchik Sep 25 '16 at 17:56
  • I know about cin, eavidan for reminding me! For most of our coding, if we don't use the system 'pause' the screen won't stay up long enough to send in a SS for the assignments – Niki M Sep 25 '16 at 17:57
  • @SamVarshavchik: You're _never_ forced. – Lightness Races in Orbit Sep 25 '16 at 18:39

2 Answers2

1
  1. You probably DON'T need "cstdlib".

  2. Instead, consider using "cin" and "cout":

http://www.cplusplus.com/doc/tutorial/basic_io/

int age;
cin >> age;
...
cout << "I am " << age << " years old.";
  1. As far as "pause", one alternative is std::cin.ignore();:

Is there a decent wait function in C++?

  1. Of course, you also need a function main() somewhere, if you don't already have one. Your example has no function, and won't compile as-is.
Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

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;                                                                   
} 
Cisplatin
  • 2,860
  • 3
  • 36
  • 56
  • Don't I need to add string and iomanip on the top as well as iostream? Or should I only use iostream for this assignment? – Niki M Sep 25 '16 at 18:04
  • This particular question only needs `iostream`. This is what gives you the `cout` and `cin` functions. `string` is for functions related to strings (we're using integers), and `iomanip` is for formatting output (which we don't need to do here). – Cisplatin Sep 25 '16 at 18:05
  • 1
    Thank you Xenon for your valuable input. I'm going to edit the coding and hope that it works. Appreciate all your help! – Niki M Sep 25 '16 at 18:08
  • @NikiM Thanks for being polite and thanking folks for there help! But while you can thank a user that helped you by leaving a comment, You can also up-vote there answer by clicking the up arrow to the left, and/or accept there answer by clicking the checkmark to the left. That can show your gratitude as well. – Christian Dean Sep 25 '16 at 19:41