Looping Error

Hey there, I'm having some trouble with a function that is supposed to prompt a user to enter the name of a customer they would like to edit. I have the customer names stored in an array. If the user input matches one of the customer names in the array, I should be prompted to edit the customers information.

Here's my function:
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
void customerEdit(string customerName[],string boatName[],float contract[],float PTD[])
{
    string userInput;
    float floatInput;
    size_t contractArrSize = sizeof(contract)/sizeof(contract[0]);
    size_t ptdArrSize = sizeof(PTD)/sizeof(PTD[0]);
    int customerArrSize = sizeof(customerName)/sizeof(customerName[0]);
    int boatNameArrSize = sizeof(boatName)/sizeof(boatName[0]);
    
    cout<<"Enter customer name you would like to edit\n";
    //read in user Input
    getline(cin,userInput);
    
    for(int i = 0; i<customerArrSize; i++)
    {
        if(userInput == customerName[i])
        {
            cout<<"which field would you like to edit(Options are: boat name, contract, PTD). \n";
            getline(cin,userInput);
            //transform(userInput.begin(),userInput.end(),userInput.begin,::tolower);
            for(int i =0; i <userInput.length(); i++)
            {
                userInput[i]=tolower(userInput[i]);
            }
            if(userInput == "boat")
            {
                cout<<"Enter a new boat name\n";
                getline(cin,userInput);
                if (userInput.length() <= 0 || userInput.length() > 15)
                {
                    cout<< "Customer name cannot be blank and must be longer than 15 chars.\n";
                    
                }
                else
                {
                   for(int i =0; i<boatNameArrSize; i++)
                   {
                       boatName[i]= userInput;
                   }
                }
            }
            else if(userInput == "contract")
            {
                cout<<"enter new contract amount\n";
                cin>>floatInput;
                cin.ignore();
                //verify if input is !<0
                if (floatInput < 0 )
                {
                    cout<< "Value entered cannot be less than 0.\n";
                    
                }
                else
                {
                    //swap element for user input
                    for(int i=0; i<contractArrSize; i++)
                    {
                        contract[i] = floatInput;
                    }
                }
                
                 
            }
            else if(userInput == "ptd")
            {
                cout<<"Enter new PTD amount\n";
                cin>>floatInput;
                cin.ignore();
                if (floatInput < 0 )
                {
                    cout<< "Value entered cannot be less than 0.\n";
                    
                }
                else
                {
                    //swap element for user input
                    for(int i=0; i<ptdArrSize; i++)
                    {
                        PTD[i] = floatInput;
                    }
                }
            }
        }
        //if user input != customerName[i]
        else
        {
            cout<<"User not found.\n";
        }
               
    }
    
}


I believe the problem is with the first control statement in the first for loop, since "User not found" is displayed after the search. I tried using the debugger, but I'm a bit too new to fully comprehend it. I am using onlinegdb IDE if that helps.
Last edited on
1
2
3
4
    size_t contractArrSize = sizeof(contract)/sizeof(contract[0]);
    size_t ptdArrSize = sizeof(PTD)/sizeof(PTD[0]);
    int customerArrSize = sizeof(customerName)/sizeof(customerName[0]);
    int boatNameArrSize = sizeof(boatName)/sizeof(boatName[0]);

Yeah, these don't work on array parameters.
The sizeof thing only works on arrays that are in scope. As soon as you pass an array to a function, all you're left with is a pointer. The size information has gone.

You need to pass the actual size as an additional parameter.

Last edited on
Here's my edit
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
void customerEdit(string customerName[],string boatName[],float contract[],float PTD[],int size)
{
    string userInput;
    float floatInput;
    
    cout<<"Enter customer name you would like to edit\n";
    //read in user Input
    getline(cin,userInput);
    
    for(int i = 0; i<size; i++)
    {
        //if user input != customerName[i]
        if(userInput != customerName[i])
        {
            cout<<"User not found.\n";
        }
        else if(userInput == customerName[i])
        {
            cout<<"which field would you like to edit(Options are: boat name, contract, PTD). \n";
            getline(cin,userInput);
            //transform(userInput.begin(),userInput.end(),userInput.begin,::tolower);
            for(int i =0; i <userInput.length(); i++)
            {
                userInput[i]=tolower(userInput[i]);
            }
            if(userInput == "boat")
            {
                cout<<"Enter a new boat name\n";
                getline(cin,userInput);
                if (userInput.length() <= 0 || userInput.length() > 15)
                {
                    cout<< "Customer name cannot be blank and must be longer than 15 chars.\n";
                    
                }
                else
                {
                   for(int i =0; i<size; i++)
                   {
                       boatName[i]= userInput;
                   }
                }
            }
            else if(userInput == "contract")
            {
                cout<<"enter new contract amount\n";
                cin>>floatInput;
                cin.ignore();
                //verify if input is !<0
                if (floatInput < 0 )
                {
                    cout<< "Value entered cannot be less than 0.\n";
                    
                }
                else
                {
                    //swap element for user input
                    for(int i=0; i<size; i++)
                    {
                        contract[i] = floatInput;
                    }
                }
                
                 
            }
            else if(userInput == "ptd")
            {
                cout<<"Enter new PTD amount\n";
                cin>>floatInput;
                cin.ignore();
                if (floatInput < 0 )
                {
                    cout<< "Value entered cannot be less than 0.\n";
                    
                }
                else
                {
                    //swap element for user input
                    for(int i=0; i<size; i++)
                    {
                        PTD[i] = floatInput;
                    }
                }
            }
        }
        
          
    }
    
}

I'm still having some issues with the name comparison. It prints out both that it did not find a match and the prompt for the next step. After the prompt for the next, nothing happens, the menu I created is prompted again for the user(I have the menu in main). If I'm understanding the debugger correctly, it sees that the user input does not match what it is the first element, it spits out that there's not match and goes back into the loop since the array is still full.
Last edited on
1
2
3
4
5
//if user input != customerName[i]
        if(userInput != customerName[i])
        {
            cout<<"User not found.\n";
        }


You only know if the name is not found when the for loop terminates without a match. Something like (not tried):

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
50
51
52
53
54
55
56
57
58
59
void customerEdit(string customerName[], string boatName[], float contract[], float PTD[], int size)
{
	string userInput;
	float floatInput;

	cout << "Enter customer name you would like to edit\n";
	//read in user Input
	getline(cin, userInput);

	for (int i = 0; i < size; ++i) {
		if (userInput == customerName[i]) {
			cout << "which field would you like to edit(Options are: boat name, contract, PTD). \n";
			getline(cin, userInput);

			//transform(userInput.begin(),userInput.end(),userInput.begin,::tolower);
			for (int i = 0; i < userInput.length(); ++i)
				userInput[i] = tolower(userInput[i]);

			if (userInput == "boat") {
				cout << "Enter a new boat name\n";
				getline(cin, userInput);

				if (userInput.length() <= 0 || userInput.length() > 15)
					cout << "Boat name cannot be blank and must be no longer than 15 chars.\n";
				else
					for (int i = 0; i < size; ++i)
						boatName[i] = userInput;

			} else if (userInput == "contract") {
				cout << "Enter new contract amount\n";
				cin >> floatInput;
				cin.ignore();

				//verify if input is !<0
				if (floatInput < 0)
					cout << "Value entered cannot be less than 0.\n";
				else
					//swap element for user input
					for (int i = 0; i < size; i++)
						contract[i] = floatInput;

			} else if (userInput == "ptd") {
				cout << "Enter new PTD amount\n";
				cin >> floatInput;
				cin.ignore();

				if (floatInput < 0)
					cout << "Value entered cannot be less than 0.\n";
				else
					//swap element for user input
					for (int i = 0; i < size; i++)
						PTD[i] = floatInput;
			}
			return;
		}
	}

	cout << "User not found.\n";
}

Last edited on
Ahhhhhh, the noobery. Thanks a ton.
Topic archived. No new replies allowed.