reading in with cin

Hey! So when i run a program, it runs but when it gets to reading in a value for cin it just ends the program without reading it. I was wondering why because i cant seem to figure it out.

1
2
3
4
5
6
7
8
9
10
11
12
  void update(string product[], int qnty[], double amount [], int &count)
{
        int id, i, quantity;
        double price;
        char ans;

        cout<< "Type in Product ID of the phone you want to update:";
        cin>>id;
        i=nextIndex(product,count);
        cout<< "Type p to edit price or q to change the quantity:";
        cin>>ans;
}


here is the code for nextIndex function in case you need it
1
2
3
4
5
6
7
8
9
10
11
12
int nextIndex(string productID[],  int &x)
{       
        int i;
        
        for(i=0; i<x; i++){
                if(productID[i]=="")
                        return i;
        }



}
Last edited on
Without a compilable example of your code, it’s hard to guess the exact problem.
When you type a number, let’s say 47, and press ENTER, you’re typing 3 data inside the input buffer. If you ‘std::cin’ them inside an integer or a char, there’s an ‘\n’ left alone in the buffer. That ‘\n’ will be read by the following input operation.
According to how your code is done, it can gives problems or not. I think this could be your issue.

Please have a look at the following hints and, if they don’t help you solving your problem, post again, but an entire compilable example which reproduces your error:
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
38
39
40
41
42
43
44
45
46
47
48
49
// So when i run a program, it runs but when it gets to reading in a value for 
// cin it just ends the program without reading it. I was wondering why 
// because i cant seem to figure it out. 
#include <iostream>
#include <string>

void update(std::string product[], int qnty[], double amount[], int &count);
int nextIndex(std::string productID[],  int &x);

int main()
{
    std::string products[] { "alpha", "beta" };
    int quantities[] { 10, 20 };
    double amounts[] { 13, 666 };
    int mycount {};
    update(products, quantities, amounts, mycount);

    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}

void update(std::string product[], int qnty[], double amount[], int &count)
{
    std::cout<< "Type in Product ID of the phone you want to update: ";
    int id {};
    std::cin >> id;
    std::cin.ignore(1); // get rid of the following '\n'
    
    int i = nextIndex(product, count); // nextIndex() shouldn't be findProduct()?

    std::cout << "Type p to edit price or q to change the quantity: ";
    char ans {};
    std::cin >> ans;
    std::cin.ignore(1); // get rid of the following '\n'
}

// What's the point of passing x as reference if it's not modified inside the function?
int nextIndex(std::string productID[],  int &x)
{       
    int i {};
    for(; i<x; i++) {
        // I think what you really want is if(productID[i] != "")
        if(productID[i] == "") { // if(productID[i].empty()) ?
            return i;
        }
    }
    // what if your if-statement doesn't match anything inside productID[]?
}

Topic archived. No new replies allowed.