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.
#include <iostream>
#include <string>
usingnamespace 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