Program 10

Hello,

I moved on with the exercises. Here is one tricky:

Write a program that converts spelled-out numbers such as "zero" and "two" into digits, such as 0 and 2. When the user inputs a number, the program should print out corresponding digit. Do it for the values 0,1,2,3, and 4 and write out not a number I know if the user enters something that doesn`t correspond, such as a stupid computer!

Now I don`t know what is the smartest way to this or what the author (perhaps) expects but here is some code I put together:

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
  #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>

using namespace std;



   int main()

      {

      string zero;
      string one;
      string two;
      string three;
      string four;
      cin>>zero;
      cin>>one;
      cin>>two;
      cin>>three;
      cin>>four;

  
  if ("<< string zero <<")

 { cout<<"0"; }

   if ("<< string one <<")

 { cout<<"1"; }

   if ("<< string two <<")

 { cout<<"2"; }

   if ("<< string three <<")

 { cout<<"3"; }

  if ("<< string four <<")

  { cout<<"4"; }

  else

  {      cout<<"Not a number I know";
      
      }


}
           
Last edited on
The best way is IMO to use an array/vector of strings with each element corresponding to the English word for its index/position , so you can use this as a map of numbers to their words and then it's easy enough from there.
Last edited on
Yes but I have not been introduced array/vector yet I`m in chapter objects, types and values. The current code, however, does not produce correct results, maybe something wrong with it?

Thank you for your reply.
Your if statements are incorrect, change them to
1
2
3
4
if(zero == "0")
{
   cout<<0<<endl;
}
The user is going to type in the word for the number though I think? I'd do something like this. You could add in a loop structure to repeat the process.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<string>

int main()

{
	std::string number;
	std::cout << "Please enter a number using letters" << std::endl;
	std::cin >> number;

	if (number == "zero")
		std::cout << "You entered 0" << std::endl;
	else if (number == "one")
		std::cout << "You entered 1" << std::endl;
		
	return 0;
}
I have not used loops before, maybe use the for-loop? I have no idea how to add a loop structure, somehow need to assign that if user enters three it is equal to 3. Or if user enters ten it is equal to 10.
How does the compiler know this? Here is what I would write:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include<iostream>
#include<string>

int main()

{
	std::string number;
	std::cout << "Please enter a number using letters" << std::endl;
	std::cin >> number;
        int a=0;

	if (number == "zero")
		std::cout <<"You entered a" << std::endl;
	else if (number == "one")
		std::cout <<"You entered a+1" << std::endl;

        
        for (std::string number == "ten"; std::string number>= "zero"; std::string  number--)
		
	return 0;
}         



and this would do the loop for numbers 0-10.
Last edited on
Whatever you put within quotes is going to get printed out as text.
In line 13 - you're going to print "You entered a". You don't really need a variable there - you're printing out a text message in response to what the user entered.

Using a loop in this example would let the user enter multiple numbers. With a for loop you need to know ahead of time how many times you're going to go through the loop. With a while loop you can let the user enter until they choose to stop.

If you want to use a for loop, you can ask the user how many numbers they want to enter, then store that number in a variable. Then for the for loop, you'd

1) initialize the loop counter to 1
2) put the condition as the counter <= the number the user entered
3) increment the counter by 1

So if the user said they want to enter 5 numbers (userNumber = 5), the counter would start at one, one is <= the userNumber, so the code within the loop runs. The counter then increments to 2, this is still <= 5, so the code in the loop runs again. And so on ... until the counter increments to 6. 6 is not <= 5, so the loop will terminate after running 5 times and the program will move to the next line of code after the loop.

1
2
3
4
5
6
for (int counter = 1; counter <= userNumber;  ++counter)
{
// your code here within the loop 
// enter a number between 0 and 4 written out in letters
// if / else statements to print out the numeric equivalent
}

I think you are still a little confused over the basic concepts , I suggest revise them .

In the cout statement you kept a inside the string so it won't be treated as a variable.
1
2
3
4
//wrong way:
std::cout <<"You entered a" << std::endl;
//the correct way :
std::cout << "You entered " << a << std::endl;


Further,this is how your main "logic" part could look like.
1
2
3
4
5
6
7
8
9
if(number == "zero")//What if the user inputs "Zero" , "ZERO" or "ZeRo"
    a=0;
else if(number =="one")
    a=1;
...
else if(number == "nine")
    a=9;
//and finally output.
std::cout << "You entered : " << a << std::endl;
Last edited on
Yes, but I`m really trying to make use of the counter operator as wildblue pointed out. I would use the while-statement which matches best with the original question and here is the code I wrote:

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
 #include<iostream>
#include<string>

int main()

{


   std::string number;
   std::cout<< "Please enter a number using letters" << std::endl;
   std::cin>> number;
   while (std::cin>> number) {
   ++number;
   
   if (number == "zero")
   std::cout<< "You entered 0" << std::endl;
  

   else if (number == "one")
   std::cout<< "You entered 1" << std::endl; }

  
		
	return 0;
}



When trying to compile getting strange error??
The variable number is the text number that the user entered. That's not the value you want to use as a counter - you can't increment a text string - that's what the compiler is complaining about.

If you're going to use a while loop, you need to let the user break out of it somehow, otherwise you have an infinite loop.

There's more info on loops here that might be helpful.
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
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
  #include<iostream>
#include<string>

int main()

{

   int number=0;
   std::string value;
   std::cout<< "Please enter a value using letters" << std::endl;
   std::cin>> value;
   while (std::cin>> value) {
   ++number;
   
   if (value == "zero")
   std::cout<< "You entered " << 0 << std::endl;
  

   else if (value == "one")
   std::cout<< "You entered " << 1 << std::endl; }

  
		
	return 0;
}


Now I can compile the program, the only problem is that it stucks after it prompts user to enter a value.
I think you're getting stuck above because the program is expecting two inputs, line 11 and then line 12. The number variable you have there just keeps incrementing indefinitely. You need a way for the user to get out of the loop. One way to do this is using a sentinel, or special value that the user enters to indicate they want to stop.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<string>

int main()
{
   std::string number;
   std::cout << "Please type a number (zero, one, two, three or four)" << std::endl;
   std::cout << "Enter Q to quit." << std::endl;
   std::cin >> number;
   while (number != "Q" && number !="q") 
   {
		if (number == "zero")
   			std::cout<< "You entered 0 " << std::endl;
   		else if (number == "one")
   			std::cout<< "You entered 1 " << std::endl; 
   		//else if for the other numbers they can enter

		std::cout << "Please type a number (zero, one, two ,three or four)" << std::endl;
   		std::cout << "Enter Q to quit." << std::endl;
   		std::cin >> number;
	}

	return 0;
}
Last edited on
closed account (j3Rz8vqX)
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
#include <iostream>
using std::cout;
using std::cin;
using std::string;
bool prompt(const string str[], const int num)
{
    cout<<"Enter one of the following strings: ";
    for(int i=0;i<num;++i)
        cout<<str[i]<<' ';
    return true;
}
bool validate(string &choice, const string str[], const int num)
{
    for(int i=0,size=choice.size();i<size;++i)
        choice[i]=tolower(choice[i]);
    for(int i=0;i<num;++i)
        if(choice==str[i])
        {
            choice = '0'+i;
            return false;
        }
    return true;
}
int main()
{
    const string display = "\nChoice: ", str[] = {"zero","one","two","three","four"};
    const int num = 5;
    string choice;

    prompt(str,num);
    while(cout<<display&&getline(cin,choice)&&validate(choice,str,num));
    cout<<"Result: "<<choice<<'\n';
    cout<<"Press <enter> to exit: ";
    cin.get();
    return 0;
}
Enter one of the following strings: zero one two three four
Choice: asdf

Choice: five

Choice: tWo
Result: 2
Press <enter> to exit:
FYI, in your given code you can quit by sending an EOF signal , in a Unix , you can do that by Ctrl + d in the terminal , idk , how it's done on Windows.
Topic archived. No new replies allowed.