Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Need help with C++


  • Please log in to reply
2 replies to this topic

#1 Usko_Detra

Usko_Detra

  • Members
  • 37 posts
  • OFFLINE
  •  
  • Local time:07:48 AM

Posted 05 September 2016 - 10:10 AM

I'm new to C++ and I'm having a bit of difficulty with a program. At the moment the program is in a loop, the user inputs a phrase/command, and the program responds. Here's what I got

 

 

for(;;){
    cout << "\nYOU: ";   //User prompt
    string phrase;
    getline(cin, phrase);   //Using getline for multiword input, then store in phrase

    cout << "COMPUTER: ";
    AI.respond(phrase);  //pass the user input to the computer and see if he can respond

 

 

This is in my main.cpp file. My question is how can I terminate the program with user input? For example, I type "goodbye," The system responds with Goodbye, then terminates the program. I've toyed around with breaks and ends, but I lack the proper knowledge to put them to proper use. I do have more of the program separated up, so yes, there is more missing, but that is the part I'm currently focusing on. Can anyone help?

 



BC AdBot (Login to Remove)

 


#2 reesesCandy

reesesCandy

  • Members
  • 2 posts
  • OFFLINE
  •  
  • Local time:07:48 AM

Posted 05 September 2016 - 03:04 PM

Assuming your in the main function

if(phrase == "goodbye" || phrase == "Goodbye") {
    cout << "Goodbye" << endl;
    return 0;
}

If you just want to exit your loop, use a

break;

You can also look into a function named: exit().


Edited by reesesCandy, 05 September 2016 - 03:05 PM.


#3 Usko_Detra

Usko_Detra
  • Topic Starter

  • Members
  • 37 posts
  • OFFLINE
  •  
  • Local time:07:48 AM

Posted 06 September 2016 - 08:34 AM

Ahh, so I was on the right track. I had figured an if, but couldn't figure out how to properly implement it. It's returning an error, but I think I can sort it out... Thanks! I have a good foundation as to where to continue.


Edited by Usko_Detra, 06 September 2016 - 09:10 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users