0

I am building a console application in C++ and I want to have 2 things:

  • when someone inputs the word "exit" to exit the console, and
  • when someone inputs "showme" to show a string I have made.

I tried to make this

#include "stdafx.h"
#include <stdio.h>
#include <iostream>

using namespace std;

    int answer;
    cout << "What do you want to learn?" << endl;
    cin << answer << endl;
    if answer == "show" 
     cout << "You have been shown the light" << endl;
    if answer == "exit"
     exit.window 

It is how I imagine the code to be, but if someone could please help, I am on my 4th C++ lesson. Thanks in advance.

4
  • 5
    I think you have missed the first 3 C++ lessons. Commented Nov 10, 2011 at 19:50
  • @KirilKirov: I'm thinking (hoping) that is meant to be pseudo-code. Commented Nov 10, 2011 at 19:51
  • @BenVoigt - I hope so, too :) Commented Nov 10, 2011 at 19:52
  • It was pseudocode indeed thats why didnt add int main and etc like parenthesis hehe :) Commented Nov 10, 2011 at 19:54

5 Answers 5

4

Here are some points:

  • You are missing the code for your main function.

  • You are trying to output << to cin, when it should be input instead >>.

  • You are trying to input an int when it should be a string.

  • You are missing parenthesis for your if statements.

  • In order to exit to the console, simply return from main or call exit(status).

Sign up to request clarification or add additional context in comments.

2 Comments

yeah i just wrote this as pseudocode to be honest didnt see it well there can i add the input coming from cin to a string and then use that string inside an if for ex. (if boom was entered to display boom boom ) ? Im still a begginer in C++
@Sinner: Yes, you can do whatever you want with the string.
3

Simplest way is just to return from main.

Comments

0

You could make use of a simple loop such as this:

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

int main() {

    string answer = "";

while(answer != "exit")
{
    cout << "What do you want to learn?";

    cin >> answer;

    if (answer = "show") 

     cout << "You have been shown the light";
}
return 0;
}

Keeping in mind that this is a pseudo code example, you should be able to continue from here and use the vast amount of resources available online to find the correct syntax to get this in working order.

Here's a great website to reference throughout your journey learning c++

cplusplus

2 Comments

Oh that's evil use of assignment inside an if. No better way to learn though.
The goal of the post was to show the implementation of the loop, nothing further. I'm sure he'll cover more topics as he progresses but perhaps handling the question at hand is the best solution in this case.
0

You need to use a string for answer, not an integer. Also the cin operator is >>, not <<.

Comments

0

Something like:

int main()
{
  std::string answer;
  std::cout << "What do you want to learn?" << std::endl;
  cin >> answer;

  if(answer == "show") 
    cout << "You have been shown the light" << endl;
  if(answer == "exit")
    return 0;
}

2 Comments

I just tried this one and it gave me an error :( im using visual studio 2010 i also added on top of this the include <iostream>
Well, this program will exit no matter what the user enters :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.