-4

I want to make a program so that when I type in 1 it will print go ahead infinite times and like that 2-go left 3-go right. And when I enter 0 it should stop everything and print !!!stop!!!

I have tried many things but I can't even make the 1 command work. Please help.

    #include "stdafx.h"
    #include<iostream>
   using std::cout;
   using std::cin;
   using std::endl;


    int main()
    {
        int value;
        cout << "Would you please enter the command " << endl;
        cout << " _______________________" << endl;
        cout << " 1- go ahead           |" << endl;
        cout << " _______________________" << endl;
        cout << " 2- make a left turn   |" << endl;
        cout << " _______________________" << endl;
        cout << " 3- make a right turn  |" << endl;
        cout << " _______________________" << endl;
        cout << " 4- go back            |" << endl;
        cout << " _______________________" << endl;
        cout << " 0- stop everything    |" << endl;
        cout << " _____________________________________________________" << endl;

        cin >> value;

        switch (value)
        {
        case 1:
            while (int value == 1)
            {
                cout << "  * go ahead " << endl;
                if (value == 0)
                {
                    break;
                }
            }
            break;
        case 2:
            cout << " * turn left " << endl;
            break;
        case 3:
            cout << " * turn right " << endl;
            break;
        case 4:
            cout << " * reverse " << endl;
            break;
        case 0:
            cout << " !!!Stop!!! " << endl;
            break;

        }


        return 0;
    }
  • 1
    `'1'` is not the same `1` – UnholySheep Jan 15 '18 at 08:35
  • what value you entered, 0 or 48 ? change the `case '1':` to `case 1:` & so on – Achal Jan 15 '18 at 08:36
  • 3
    Also `for (int value == 1)` is not valid C++ and shouldn't compile – UnholySheep Jan 15 '18 at 08:36
  • You really ought to avoid `using namespace std` - it is a bad habit to get into, and [can silently change the meaning of your program](/q/1452721) when you're not expecting it. Get used to using the namespace prefix (`std` is intentionally very short), or importing *just the names you need* into the *smallest reasonable scope*. – Toby Speight Jan 18 '18 at 10:58

3 Answers3

3

for(value==1) is no valid C++ syntax and even if it would be, your code would print "go ahead" infinitely after 1 has been entered.
Besides that, your switch cases are checking for ASCII 1 instead of numeric 1. Change '1' to 1.

You need to surround your input switch statement with some kind of loop like this:

int value = 0;
do{
    cin >> value;
    switch(value){
        case 1:
            cout << "go ahead" << endl;
            break;
        case 2:
            cout << "turn left" << endl;
            break;
        case 3: 
            cout << "turn right" << endl;
            break;
        case 4:
            cout << "reverse" << endl;
            break;
        case 0:
            cout << "stop" << endl;
            break;            
    }    
}while(value!=0);

This Code will ask for input and print the direction afterwards.

Maku
  • 199
  • 2
  • 15
  • Well the problem is that the "go ahead ", "turn left, right" and "reverse" signs have to be continuously written (I mean like this while(true){cout<<"Go ahead "< – David Danielyan Jan 17 '18 at 18:00
  • As far as i've read it's not possible in standard c++ as it's terminal/os dependant. This might help you: https://stackoverflow.com/questions/421860/capture-characters-from-standard-input-without-waiting-for-enter-to-be-pressed – Maku Jan 18 '18 at 07:03
3

You have three problems. Firstly, '1' is a character literal and 1 is an integer literal (and I know of no implementation where they have the same value). You need to get rid of the quotes.

Secondly, for (int value == 1) is not valid C++. You need to change this to while (value == 1).

Finally, and most significantly, nothing inside the loop will allow value to change. It will be an infinite loop (and hence undefined behaviour). You will need to run the indefinite loop on one thread, and do input in a loop on the other thread.

(Also, please don't use using namespace std;)

1

remove '' in case statements

switch (value)
    {
    case 1:
        for (value == 1)
        {
            cout << "  * go ahead " << endl;
            if (value == 0)
            {
                break;
            }
        }
        break;
    case 2:
        cout << " * turn left " << endl;
        break;
    case 3:
        cout << " * turn right " << endl;
        break;
    case 4:
        cout << " * reverse " << endl;
        break;
    case 0:
        cout << " !!!Stop!!! " << endl;
        break;

    }
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22
  • 1
    The `for (value == 1)` thing is still here. –  Jan 15 '18 at 08:46
  • `for (value == 1)` ? `for` or `if` ? – Achal Jan 15 '18 at 08:51
  • since the OP wants it printed "infinite times", it seems `for` is correct – O.O.Balance Jan 15 '18 at 08:53
  • I am sorry I didn't finish editing before uploading I was still editing the code. But the main problem here is that the "go ahead ", "turn left, right" and "reverse" signs have to be continuously written (I mean like this while(true){cout<<"Go ahead "< – David Danielyan Jan 17 '18 at 18:02