0

Hell all. This problem is driving me up the wall as I cant figure out what i'm doing wrong. All I want is to collect to variables and pass them into a function, however as soon as a input the first variable the function runs?

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void avg_rain(string, string);
int main()
{
    string file, unit;

    //Prompt user for file names, and assign to character array
    cout << "Please type the file names with extentions that you would like to open, seperate file names with spaces. \nfile name(s) :";
    cin >> file;

    //Prompt user for measurement unit
    cout << "Please type the unit you would like measurments to be converted to (mm, cm, in)\nUnit : ";
    cin >> unit;

    //execute function with passed through parameters
    avg_rain(file, unit);

    system("pause");
return 0;
}

void avg_rain(string ffile ,string funit )
{
    cout << ffile;
}

Even though don't type anything for the second variable, this is my output.

[OUTPUT:]

Please type the file names with extentions that you would like to open, seperate file names with spaces.

file name(s) :hello world
Please type the unit you would like measurments to be converted to (mm, cm, in)

Unit : hello

Press any key to continue . . .
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    The assumption begged in the title is "not possible". – user2864740 Apr 04 '18 at 21:51
  • 2
    It's because of the space in the file name. Use [`getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline). – 001 Apr 04 '18 at 21:51
  • Hell all - probably not a good way to start a post. But then again I am a queer atheist poof. So I will be going to he'll. – Ed Heal Apr 04 '18 at 21:57
  • `>>`'s default behaviour is to stop after a single whitespace-delimited token. Basically it reads one and only one word. You want behaviour more like Option 2 in this linked answer: https://stackoverflow.com/a/7868998/4581301 – user4581301 Apr 04 '18 at 21:57
  • It seems that getline has fixed the issue! thanks very much, I spent way to much time trying to figure out this issue! – Christian Potts Apr 04 '18 at 21:58

0 Answers0