failed string subscript out range

problem occur when string name is inserted.. getting error string subscript out of range when the 1st input pop out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void checkProductNum(float prodNumber[],float prixProd[],int number, string name){
	float prodNumberTemp;
	for (int i=0;i<number;i++)	{
		do{
			cout<<"Enter the name for this item:";
			cin>>name[i];
			cout<<"Entrer le product number: ";
			cin>>prodNumberTemp;
		}while(numExists(prodNumber,number,prodNumberTemp));
	prodNumber[i] = prodNumberTemp;
	cout<<"Enter the price for this item:";
	cin>>prixProd[i];
	
	}
}
I think you want to pass name in as a string array.
pass name in as string array? what did u mean?
Put [] after string name.
tried.. there small error at cin>>name[i]; .. it said theres no operator ">>" matches this operand
hes my full code btw.. i still havent found anythings.. maybe its wrong with parameter or pointer pass.. or i missed smth

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <stdlib.h>
#define NL cout<<endl;
using namespace std;


void checkProductNum(float prodNumber[],float prixProd[],int number, string name[]);
void findDetail(float prodNumberArray[],float prodPrice[],float prodNumber, int number);
bool numExists (float prodNumberProd[],int number,int chiffre);
void arraySort(float prodNumberArray[],float prodPrice[],int number, int choice);
void main()
{
	int number;
	float prodNumber;
	int choice=0;
	float *prodNumberArray;
	float *prodPrice;
	string name[];
	
	cout<<"How many items will you enter: ";
	cin>>number;
	prodNumberArray = new float[number];
	prodPrice = new float[number];
	checkProductNum(prodNumberArray,prodPrice,number,name);
	system("cls");

	while (choice != 4){
		cout<<"Choose from the following: ";NL;NL;
		cout<<"1 - View products by code number.";NL;
		cout<<"2 - View products by price.";NL;
		cout<<"3 - View a product detail.";NL
		cout<<"4 - Quit";NL;
		cout<<"Enter your choice: ";NL;
		cin>>choice;
		
		switch(choice){
		case 1:
			system("cls");
			arraySort(prodNumberArray,prodPrice,number,choice);
			break;
		case 2:
			system("cls");
			arraySort(prodPrice,prodNumberArray,number,choice);
			break;
		case 3:
			cout<<"Prix de quel produit: ";
			cin>>prodNumber;
			findDetail(prodNumberArray,prodPrice,prodNumber,prodNumber);NL;
			break;
		case 4:
			system("cls");
			delete prodNumberArray;
			delete prodPrice;
			break;
		default:
			system("cls");
			cout<<"Invalid entry dumn ass!";
			break;
		}
	}
}


void checkProductNum(float prodNumber[],float prixProd[],int number, string name[]){
	float prodNumberTemp;
	for (int i=0;i<number;i++)	{
		do{
			cout<<"Enter the name for this item:";
			cin>>name[i];
			cout<<"Entrer le product number: ";
			cin>>prodNumberTemp;
		}while(numExists(prodNumber,number,prodNumberTemp));
	prodNumber[i] = prodNumberTemp;
	cout<<"Enter the price for this item:";
	cin>>prixProd[i];
	
	}
}

bool numExists (float prodNumberProd[],int number,int chiffre){
	for(int i=0;i<number;i++){
		if(prodNumberProd[i] == chiffre)
			return true;
	}
	return false;
}

void findDetail(float prodNumberArray[],float prodPrice[],float prodNumber, int number){
	if (numExists(prodNumberArray,number,prodNumber)== true){
		for (int i=0;i<number;i++)
			if (prodNumberArray[i] == prodNumber){
				cout<<"The price of "<<prodNumberArray[i]<<" is "<<prodPrice[i]<<"$";NL;
				system("pause");
				system("cls");
			}
	}
	else{
		cout<<"The entered product number does not exist.";NL;
		system("pause");
		system("cls");
	}
}

void arraySort(float prodNumberArray[],float prodPrice[], int number, int choice){
	float tempCode;
	float tempPrix;
	for (int i=0; i<(number-1);i++){
		for(int j=i+1;j<number; j++){
			if (prodNumberArray[i]>prodNumberArray[j]){
				tempCode=prodNumberArray[i];
				tempPrix=prodPrice[i];
				prodNumberArray[i]=prodNumberArray[j];
				prodPrice[i]=prodPrice[j];
				prodNumberArray[j]=tempCode;
				prodPrice[j]=tempPrix;
			}
		}
	}
	if (choice == 1){
		for(int i=0;i<number;i++){
			cout<<"Code:"<<prodNumberArray[i]<<"----";
			cout<<"Price:"<<prodPrice[i]<<"$";NL;
		}
	}
	else if (choice == 2){
		for(int i=(number-1);i>=0;i--){
			cout<<"Price:"<<prodNumberArray[i]<<"$ ----";
			cout<<"Code:"<<prodPrice[i];NL;
		}
	}
}
closed account (Dy7SLyTq)
line 18 is your problem
remove the []?
closed account (Dy7SLyTq)
you can but in c/c++ arrays have to have a set int size, determined with a literal, const int, or #define
string name[30].. the string subscript is gone.. but theres another problem .. once i enter the string name and try to go another input, the program is stop working
closed account (Dy7SLyTq)
is all you did is add 30 let me take a look
here's

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <stdlib.h>
#define NL cout<<endl;
using namespace std;


void checkProductNum(float prodNumber[],float prixProd[],int number, string name);
void findDetail(float prodNumberArray[],float prodPrice[],float prodNumber, int number);
bool numExists (float prodNumberProd[],int number,int chiffre);
void arraySort(float prodNumberArray[],float prodPrice[],int number, int choice);
void main()
{
	int number;
	float prodNumber;
	int choice=0;
	float *prodNumberArray;
	float *prodPrice;
	string name[30];
	
	cout<<"How many items will you enter: ";
	cin>>number;
	prodNumberArray = new float[number];
	prodPrice = new float[number];
	checkProductNum(prodNumberArray,prodPrice,number,name);
	system("cls");

	while (choice != 4){
		cout<<"Choose from the following: ";NL;NL;
		cout<<"1 - View products by code number.";NL;
		cout<<"2 - View products by price.";NL;
		cout<<"3 - View a product detail.";NL
		cout<<"4 - Quit";NL;
		cout<<"Enter your choice: ";NL;
		cin>>choice;
		
		switch(choice){
		case 1:
			system("cls");
			arraySort(prodNumberArray,prodPrice,number,choice);
			break;
		case 2:
			system("cls");
			arraySort(prodPrice,prodNumberArray,number,choice);
			break;
		case 3:
			cout<<"Prix de quel produit: ";
			cin>>prodNumber;
			findDetail(prodNumberArray,prodPrice,prodNumber,prodNumber);NL;
			break;
		case 4:
			system("cls");
			delete prodNumberArray;
			delete prodPrice;
			break;
		default:
			system("cls");
			cout<<"Invalid entry dumn ass!";
			break;
		}
	}
}


void checkProductNum(float prodNumber[],float prixProd[],int number, string name[]){
	float prodNumberTemp;
	for (int i=0;i<number;i++)	{
		do{
			cout<<"Enter the name for this item:";
			cin>>name[i];
			cout<<"Entrer le product number: ";
			cin>>prodNumberTemp;
		}while(numExists(prodNumber,number,prodNumberTemp));
	prodNumber[i] = prodNumberTemp;
	cout<<"Enter the price for this item:";
	cin>>prixProd[i];
	
	}
}

bool numExists (float prodNumberProd[],int number,int chiffre){
	for(int i=0;i<number;i++){
		if(prodNumberProd[i] == chiffre)
			return true;
	}
	return false;
}

void findDetail(float prodNumberArray[],float prodPrice[],float prodNumber, int number){
	if (numExists(prodNumberArray,number,prodNumber)== true){
		for (int i=0;i<number;i++)
			if (prodNumberArray[i] == prodNumber){
				cout<<"The price of "<<prodNumberArray[i]<<" is "<<prodPrice[i]<<"$";NL;
				system("pause");
				system("cls");
			}
	}
	else{
		cout<<"The entered product number does not exist.";NL;
		system("pause");
		system("cls");
	}
}

void arraySort(float prodNumberArray[],float prodPrice[], int number, int choice){
	float tempCode;
	float tempPrix;
	for (int i=0; i<(number-1);i++){
		for(int j=i+1;j<number; j++){
			if (prodNumberArray[i]>prodNumberArray[j]){
				tempCode=prodNumberArray[i];
				tempPrix=prodPrice[i];
				prodNumberArray[i]=prodNumberArray[j];
				prodPrice[i]=prodPrice[j];
				prodNumberArray[j]=tempCode;
				prodPrice[j]=tempPrix;
			}
		}
	}
	if (choice == 1){
		for(int i=0;i<number;i++){
			cout<<"Code:"<<prodNumberArray[i]<<"----";
			cout<<"Price:"<<prodPrice[i]<<"$";NL;
		}
	}
	else if (choice == 2){
		for(int i=(number-1);i>=0;i--){
			cout<<"Price:"<<prodNumberArray[i]<<"$ ----";
			cout<<"Code:"<<prodPrice[i];NL;
		}
	}
}
Think your problem is with line 7. You need to indicate that you intend to pass a name[] instead of name. So it should say

 
void checkProductNum(float prodNumber[],float prixProd[],int number, string name[]);


Another thing. It's not causing your problem but since you dynamically allocated memory for prodNumberArray and prodPrice, you should probably do the same thing with name as well instead of just putting [30] there. I would replace line 18 with:

 
string* name = new string[number];
tried.. there small error at cin>>name[i]; .. it said -theres no operator ">>" matches this operand -
Hmm. The operator>>() is overloaded to support string class strings. It doesn't look like you included the string.h or cstring preprocessor directive. Depending on your compiler, you should include the following in the first few lines of your program:

 
#include <cstring>    // for newer compilers 


Or:

 
#include <string.h>   // for older compilers 



Also, it may be helpful to replace your cin >> name[i] syntax with the getline(cin, name[i]) function.
Last edited on
Topic archived. No new replies allowed.