Andrea Garau
Andrea Garau

Reputation: 65

Why this program crashes after the first loop?

I'm reading Stroustrup's book to learn C++.

There is an exercise:

Make a vector holding the ten string values "zero" , "one" , . . . "nine" . Use that in a program that converts a digit to its corresponding spelled-out value; e.g., the input 7 gives the output seven . Have the same program, using the same input loop, convert spelled-out numbers into their digit form; e.g., the input seven gives the output 7 .

I've done it, and here is the code:

#include <string>
#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<string> numbers = {"zero", "one", "two",   "three", "four",
                              "five", "six", "seven", "eight", "nine"};
    cout << "Insert a number: \n";

    int number = 0;

    while (cin >> number) {
        cout << "\n" << number << "\t" << numbers[number] << "\n";
    }

    cout << "\nInsert a number spelled: \n";

    string num;
    while (cin >> num) {
        for (int i = 0; i = numbers.size(); i++) {
            if (num == numbers[i]) {
                cout << "\n" << num << "\t" << numbers[i] << "\n";
            }
        }
    }

return 0;
}

The problem is that when I run it, the first part goes (if I enter 7 it says "seven", but when I insert | to close the first while cycle, the program crashes.

Why is that error occurring?

P.S.: Sorry if I've made some grammar errors but I'm not English.

Upvotes: 1

Views: 1017

Answers (3)

fazzkusiak
fazzkusiak

Reputation: 1

Above one is wrong in my opinion, finally I found better solution

using namespace std;

vector <string> numbers;
void init_numbers()
{
numbers.push_back("zero");
numbers.push_back("one");
numbers.push_back("two");
numbers.push_back("three");
numbers.push_back("four");
numbers.push_back("five");
numbers.push_back("six");
numbers.push_back("seven");
numbers.push_back("eight");
numbers.push_back("nine");  
}

int get_number()
{
const int not_symbol = numbers.size();
int val = not_symbol;

if (cin >> val)
{
    return val;
}

cin.clear();
string s;
cin >> s;

for (int i = 0; i < numbers.size(); ++i)
    if (numbers[i] == s)
    {
        val = i;
    }


if (val == not_symbol)
{
    ("there is no number like ", s);
}

return val;
}

int main()

{
init_numbers();

    cout << "type your digit \n";
    int val = get_number();

    cout << "your number is " << val << "\n";


 }

Upvotes: 0

fazzkusiak
fazzkusiak

Reputation: 1

I found solve to this issue, however there are 2 string vectors

// libraries
using namespace std;
int main()
{

vector<string> values1{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
vector<string> values2{ "zero", "one", "two", "three", "four", "five", "six","seven", "eight", "nine" };

char loop = 'y';

cout << "This program spells out digits or converts them.\n";

while (loop == 'y')
{ 
cout << "Please enter a digit between 0 - 9 to spell out or write a digit to convert to a number: \n";
string digit;
cin >> digit;

for (int i = 0; i < values1.size(); ++i)
{
if (digit == values1[i])
{
cout << values2[i] << endl;
}
else if (values2[i] == digit)
{
cout << values1[i] << endl;
}
}

cout << "Would you like to continue? y / n \n";
cin >> loop;
}




}

Upvotes: 0

Ziezi
Ziezi

Reputation: 6467

Not considering the silly mistake (= instead of ==) in the for loop , currently your result is due to the fact that your are exiting the first while loop by typing wrong input format (| instead of the expected int) which causes cin to be in bad state that persists to the next while loop forbidding further input reading.

If you want your program to continue after the first while loop you should define a termination condition, i.e. an input symbol for which your while loop breaks. Additionally, you should include checking (and clearing) of cin stream states.

Upvotes: 3

Related Questions