where is the mistake???

I made a small program and i don`t know why i got a runtime error.The compiler doesn`t find anything wrong.Here is the code:
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
//chapter 4 ,ex 6 -the program will read the numbers from 0-9 into words and typing them out
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	int i =0;
	string number="";
	vector<string>numbers(10);
	numbers[0] ="zero";
	numbers[1] ="one";
	numbers[2] ="two";
	numbers[3] ="three";
	numbers[4] ="four";
	numbers[5] ="five";
	numbers[6] ="six";
	numbers[7] ="seven";
	numbers[8] ="eight";
	numbers[9] ="nine";

	cout <<"Please enter a number between 0-9 with letters: ";
	while (cin >> number){
		for(i=0;i<11;++i){
			if (number == numbers[i])
				cout << i<<endl;
		}
	}
}


the correct code is "
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
//chapter 4 ,ex 6 -the program will read the numbers from 0-9 into words and typing them out
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	int i =0;
	string number="";
	vector<string>numbers(10);
	numbers[0] ="zero";
	numbers[1] ="one";
	numbers[2] ="two";
	numbers[3] ="three";
	numbers[4] ="four";
	numbers[5] ="five";
	numbers[6] ="six";
	numbers[7] ="seven";
	numbers[8] ="eight";
	numbers[9] ="nine";

	cout <<"Please enter a number between 0-9 with letters: ";
	while (cin >> number){
		for(i=0;i< numbers.size()-1;++i){ //I don`t have numbers[10] -thats the mistake
			if (number == numbers[i])
				cout << i<<endl;
		}
	}
}
Last edited on
You took the trouble to make sure that you used only numbers[0] to numbers[9] when entering the values,
yet when you make the check loop, your loop counter goes from 0 to 10.
numbers[10] is an invalid index
Last edited on
That's a very strange piece of code to be an example from a book or tutorial.

Without being the advocate user of STL, I would have written it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    int number = 0;
    vector<string> numbers;
    numbers[0] = "zero";
    numbers[1] = "one";
    numbers[2] = "two";
    numbers[3] = "three";
    numbers[4] = "four";
    numbers[5] = "five";
    numbers[6] = "six";
    numbers[7] = "seven";
    numbers[8] = "eight";
    numbers[9] = "nine";

    cout << "Number please:  ";
    while (cin >> number) //Not 100% sure about this way of doing it.
    {
        cout << numbers[number];
    }
#ifdef DEBUG
    //Pause using your favorite method.
#endif
}


I changed the data type of number to int so the user can type the integer as a number. The way you have it assumes the number will be typed as a string (i. e. seven as opposed to 7).

If the number as a string thing is a requirement, then go back to your inner for loop. But the vector numbers should have a method to find the string so you don't have to code the inner loop. Don't ask me how because I barely use STL (I know! For the nth time, shame on me! :-) ).
Last edited on
you can use switch to resolve.
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
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;

void main(){
	char number;
	int cond=1;
	cout<<"press e to exit"<<endl;
	while(cond!=0){
	cout<<"number = ";
	cin>>number;
	system("cls");
	switch(number){
	case '0':
	cout<<"zero";
	break;
	case '1':cout<<"one"<<endl;
	break;
	case '2': cout<<"two"<<endl;
	break;
	case '3': cout<<"three"<<endl;
	break;
	case '4': cout<<"four"<<endl;
	break;
	case '5': cout<<"five"<<endl;
	break;
	case '6': cout<<"six"<<endl;
	break;
	case '7': cout<<"seven"<<endl;
	break;
	case '8': cout<<"eight"<<endl;
	break;
	case '9': cout<<"nine"<<endl;
	break;
	case 'e': cond=0;
	default:
		cout<<"not a number"<<endl;
	}
	}
	_getch();
}
}
Last edited on
You never tell the vector to allocate space for 10 values.
Construct it like this:
vector<string> numbers(10);
Or use push_backs().
Actually the user types for example "zero" and the answer should be "0"
Thanks for pointing my mistake - i always forgot the vector starts from 0...
Ok the problem is solved - i was trying to test vector`s[10] when i don`t have such.
Topic archived. No new replies allowed.