What is going wrong in the for loop when I try to get data from user

What is wrong with the for loop that is getting cname and camount from the user.

Here is my input: (Note base on the size I entered, this loop should loop 3 times and ask for 3 different names and 3 different amounts, but instead it ask me for the first name and then it exits the loop. I think this has something to do with the cin statements, but I can't figure it out.

size = 3

Name = "Asha R"

It throws me out of the loop and exits the program. I should be able to enter 3 names and 3 amounts based on the size I entered in the first statement.

I've tried to change the cin statements several different ways, but nothing is working. Your help is much appreciated:

#include <iostream>
#include <string>

using namespace std;

struct contribution {

string cname;
double camount = 0.0;
};


int main ()
{


int size = 0;

//get array size from user
cout << "How many members do you want to enter" << endl;
cin >> size;

//create dynamic array of contribution structure
contribution * conarr = new contribution[size];

//This is my problem area. When I enter the 1st loop and enter "Asha R" it kicks out of the loop. I tried to change the cin statements several different ways, but it's not working.

for (int i=0; i < size; i++)
{
cout << "Enter name: " ;
//getline(cin, conarr[i].cname);
getline(cin,conarr[i].cname).get();
cout << "Enter contribution amount: ";

// cin >> conarr[i].camount;
(cin >> conarr[i].camount).get();

}

delete[] conarr;
}
google cin and getline. the two tools do not play nice together. the result will show you what to do... you need an ignore() ...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>

using namespace std;

struct contribution
{
    string cname;
    double camount = 0.0;
};

int main ()
{
    int size = 0;
    cout << "How many members do you want to enter: ";
    cin >> size;
    
    contribution* conarr = new contribution[size];
    
    for (int i = 0; i < size; i++)
    {
        std::cin.ignore(1000, '\n'); // <-- AS PREVIOUS MENTION
        
        cout << "Enter name: " ;
        getline(cin, conarr[i].cname);
        
        cout << "Enter contribution amount: ";
        cin >> conarr[i].camount;
    }
    
    for (int i = 0; i < size; i++)
    {
        std::cout << conarr[i].cname << ' ' << conarr[i].camount << '\n';
    }
    
    delete[] conarr;
}


How many members do you want to enter: 2
Enter name: Betty BOOP
Enter contribution amount: 23.14
Enter name: G Wolf
Enter contribution amount: 0.01
Betty BOOP 23.14
G Wolf 0.01
Program ended with exit code: 0


Another way is to use the stream manipulator ws to ignore white space:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct contribution
{
	string cname;
	double camount {};
};

int main() {
	size_t size {};

	cout << "How many members do you want to enter: ";
	cin >> size;

	const auto conarr {new contribution[size]};

	for (size_t i = 0; i < size; ++i) {
		cout << "Enter name: ";
		getline(cin >> ws, conarr[i].cname);

		cout << "Enter contribution amount: ";
		cin >> conarr[i].camount;
	}

	for (size_t i = 0; i < size; ++i)
		cout << conarr[i].cname << ' ' << conarr[i].camount << '\n';

	delete[] conarr;
}

Thank you very much! I havent been introduced to ignore yet, but it makes sense. The 2nd example is what I was missing. Thank. you all for helping me!
Topic archived. No new replies allowed.