0

I am simply trying to get the input from the user for the name of the ski resort to display fully with white spaces. I have tried multiple things including setting a constant for size, using cin.get and cin.getline. Have had no luck thusfar. Also directly below it, I am trying to set the parameters on the rand function using information entered by user previously in minimumSnowfall and maximumSnowfall.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>

using namespace std;


int main(void)

{
    int minimumSnowfall;
    int maximumSnowfall;
    char skiResortName[81];


    cout << "I will forecast how much snowfall there will be on each \nday this weekend. You get to pick the range. " << endl;

    cout << "Please enter the minimum amount of snow that \nwill fall in one day in inches: ";
    cin >> minimumSnowfall;


    cout << "Please enter the maximum amount of snow that \nwill fall in one day in inches: ";
    cin >> maximumSnowfall;

    // need to fix to display names with white spaces
    cout << "Enter the name of the ski resort: ";
    cin.getline(skiResortName,81);
    cout << "The local forecast for snowfall at " << skiResortName << " this weekend: " << endl;

    // need to fix to display random number using parameters entered by weatherman in minimumSnowfall and maximumSnowfall
    cout << "Friday: " rand  (minimumSnowfall, maximumSnowfall);







    return 0;
  • Explain this line: `cout << "Friday: " rand % minimumSnowfall, maximumSnowfall);` Where is your `getline` btw? – LogicStuff Feb 13 '16 at 16:39
  • It is not proper code. I have no idea how to set parameters using the input from the user for those two ints. –  Feb 13 '16 at 16:41
  • The code doesn't seem to match your question. There's no get line in there anywhere. – David Hoelzer Feb 13 '16 at 16:44
  • @Jaeden By *"cin.getline not waiting for input and setting parameters on rand"* you mean *"how to print a random number between two values"*? Because it does not mean exactly the same thing. – LogicStuff Feb 13 '16 at 16:45
  • @LogicStuff exactly right, between the two values inputted previously by the user in minimumSnowfall and maximumSnowfall. –  Feb 13 '16 at 16:48
  • http://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range – LogicStuff Feb 13 '16 at 16:50
  • @LogicStuff can you interpret this in simple terms? I am not a programming major and do not speak the language of programmers yet (: –  Feb 13 '16 at 17:22

1 Answers1

2

This is a common problem of reading string input after an int/long/float/etc. The issue is the '\n' character remaining in the buffer after an integer has been read:

cin >> maximumSnowfall;

When you call getline the '\n' still remaining in the buffer gets read right away. The standard library interprets it as an empty string, and returns right away.

To avoid this issue call ignore after reading an int:

cin >> maximumSnowfall;
cin.ignore(1, '\n');
Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399