0

So I making this code but I don't know what's wrong

    int ArrSize = 2;
    int AccNum[ArrSize];
    string AccName[ArrSize];
    double AccBal[ArrSize];
    
    cout << "********** ENTER ACCOUNT **********" << endl;
        for(int i = 0; i < ArrSize; i++){
            
            cout << "Enter Account Number: ";
            cin >> AccNum[i];
            
            cout << "Enter Account Name: "; 
            getline(cin, AccName[i]);
            
            cout << "Enter Amount: ";
            cin >> AccBal[i];
            cout << "***********************************" << endl;  
        }
            cout << endl;

When I use the getline I get a result like this

********** ENTER ACCOUNT **********
Enter Account Number: 123
Enter Account Name: Enter Amount:

But, when I use the cin >> AccName[i]; I don't get a problem. I want to use the getline so I can put a string with space. How can I do that?

JaMiT
  • 14,422
  • 4
  • 15
  • 31
Punkkkk
  • 21
  • 3
  • 1
    See the duplicate question for more information. Also: `int AccNum[ArrSize];` - whichever C++ textbook showed you to do this -- you need to throw it away immediately, and get a different C++ textbook. If you copied that off some web site, don't visit that web site any more. If you saw this in some clown's Youtube video, unsubscribe from that channel, you're not learning proper C++. This is not standard C++, and many C++ compilers will refuse to compile this. – Sam Varshavchik Nov 29 '22 at 04:40
  • 1
    @SamVarshavchik In this case, trashing the textbook might be a bit harsh. Forgetting the `const` in front of `int ArrSize = 2;` is an easy mistake to make, especially if the compiler does not complain. This is rather different than the examples that read the array size from the user. – JaMiT Nov 29 '22 at 05:56

0 Answers0