d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Hi I Have A Simple Question
12Next
Add Reply New Topic New Poll
Member
Posts: 1,086
Joined: May 7 2013
Gold: 0.00
Aug 27 2017 02:57am
I want to program a simple console application where I can type commands in like for example "help" to make it print out a certain stuff and I would like to have the code and I just simply can not get it and I am looking now for long.
If some one can help me it would be really nice.
Thank you and regards.
Member
Posts: 1,263
Joined: Feb 11 2008
Gold: 2,650.00
Aug 27 2017 02:46pm
Relevant references:
http://www.cplusplus.com/reference/string/string/getline/
http://www.cplusplus.com/reference/string/string/compare/

Code

#include <iostream>
#include <string>

int main()
{
std::string input = "";
std::cout << "Enter command: ";
std::getline(std::cin, input);

if (input.compare("help") == 0)
{
std::cout << "Help information xxxxxx";
}
else if (input.compare("command 1") == 0)
{
std::cout << "yyyyyy";
}
else
{
std::cout << "No recognized command entered";
}
}
Member
Posts: 1,086
Joined: May 7 2013
Gold: 0.00
Aug 27 2017 06:06pm
Thank you very much. It worked and I understood it. Do you maybe know what I have to add to make it not shut itself down anymore?
Member
Posts: 23,502
Joined: Aug 3 2011
Gold: 6,921.00
Aug 27 2017 06:46pm
Quote (John_Locke @ Aug 27 2017 05:06pm)
Thank you very much. It worked and I understood it. Do you maybe know what I have to add to make it not shut itself down anymore?


Quote (postmortemvox @ Aug 27 2017 01:46pm)
Relevant references:
http://www.cplusplus.com/reference/string/string/getline/
http://www.cplusplus.com/reference/string/string/compare/

Code
#include <iostream>
#include <string>

int main()
{
std::bool exit = false;
std::string input = "";
std::cout << "Enter command: ";
std::getline(std::cin, input);

do
{
if (input.compare("help") == 0)
{
std::cout << "Help information xxxxxx";
}
else if (input.compare("command 1") == 0)
{
std::cout << "yyyyyy";
}
else
{
std::cout << "No recognized command entered";
}
} while(exit = true)
system("pause");
}


Will make it so the program doesn't close or you can use a do-while loop.

Member
Posts: 1,263
Joined: Feb 11 2008
Gold: 2,650.00
Aug 27 2017 08:46pm
Quote (John_Locke @ Aug 27 2017 07:06pm)
Thank you very much. It worked and I understood it. Do you maybe know what I have to add to make it not shut itself down anymore?


Wrap the entire contents of the main() function in a while(true) and put in an "exit" command to break out of the loop:
http://en.cppreference.com/w/cpp/language/break

Code
#include <iostream>
#include <string>

int main()
{
while (true)
{
std::string input = "";
std::cout << "Enter command: ";
std::getline(std::cin, input);

if (input.compare("help") == 0)
{
std::cout << "Help information xxxxxx";
}
else if (input.compare("command 1") == 0)
{
std::cout << "yyyyyy";
}
else if (input.compare("exit") == 0)
{
break;
}
else
{
std::cout << "No recognized command entered";
}
}
}
Member
Posts: 1,086
Joined: May 7 2013
Gold: 0.00
Aug 28 2017 02:18am
Killer man. Thank you.
Member
Posts: 1,086
Joined: May 7 2013
Gold: 0.00
Aug 28 2017 07:44am
Quote (Cocoo @ Aug 27 2017 02:46pm)
Will make it so the program doesn't close or you can use a do-while loop.


It did not work. I do not understand the code. What is wrong?





This post was edited by John_Locke on Aug 28 2017 07:55am
Member
Posts: 1,086
Joined: May 7 2013
Gold: 0.00
Aug 28 2017 08:03am
Why line 6 error: expected ';' before 'bool'



I do not understand this. What does that mean 'exit' was not declared in this scope.
Member
Posts: 3,197
Joined: May 4 2013
Gold: 1,457.00
Aug 28 2017 10:58am
There is no std::bool type. There is bool.

also this code is so bad on multiple levels..., instead of trying to blindly copypaste stuff from the internet, get a good book.

This post was edited by nuvo on Aug 28 2017 11:02am
Member
Posts: 1,086
Joined: May 7 2013
Gold: 0.00
Aug 28 2017 01:00pm
Quote (nuvo @ Aug 28 2017 06:58am)
There is no std::bool type. There is bool.

also this code is so bad on multiple levels..., instead of trying to blindly copypaste stuff from the internet, get a good book.


I have many books and they have all thousands of pages and I am looking for long now and still just do not get them.
I am not trying to blindly copypaste I try to understand but for that it must function and then I can learn and put my own content inside.
All I want to know is how can I get a line out of a string compare to use it in the cout.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll