0

I am trying to change the values of variables repeatedly in the main function using a for loop, the first time it works fine but on the second iteration it skips the getline command and goes to the next line. I am still new to programming and not experienced.

#include <iostream>
#include <string>
using namespace std;

void getname(string &ss, float &num)
{

    cout << "Type name "<< endl;

    getline(cin, ss, '\n');
    cout << "enter number " << endl;
    cin >> num;
}

int main()
{
    string ss;
    float num;
    for (int i = 0; i < 3; i++)
    {
        getname(ss, num);
        cout << ss << endl;
        cout << num << endl;
    }
    return 0;
}
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
Gobabis
  • 97
  • 3

0 Answers0