Good evening to all at GameDev im very much a noob to C++ and i am looking for some advice to my issue
below is the source to a small program where i get the user to enter there name and then there name is displayed back to them on the screen all this works 100% fine but the main issue is that the console window closes and opens very fast below is the source code
#include <iostream>
#include <string>
int main()
{
std::string FirstName;
std::cout << "Please enter your first name:";
std::cin >> FirstName;
std::cout << FirstName << "\n";
std::cin.get();
return 0;
}
Thank you very much in advance
The correct way to deal with this is to run the application from the console (adding a cin.ignore(); followed by cin.getline(); works for simple tests but will make the program a pain to use in shell scripts.
consider for example an application that takes a set of strings, sorts them and prints the sorted list
#include <iostream>
#include <list>
#include <string>
bool compare_nocase (string first, string second)
{
unsigned int i=0;
while ( (i<first.length()) && (i<second.length()) )
{
if (tolower(first)<tolower(second)) return true;
else if (tolower(first)>tolower(second)) return false;
++i;
}
if (first.length()<second.length()) return true;
else return false;
}
void main() {
std::string tempString;
std::list<std::string> stringList;
while (std::cin >> tempString) {
stringList.push_back(tempString);
}
stringList.sort(compare_nocase);
std::list<std::string>::iterator it;
for (it=stringList.begin();it!=stringList.end(); ++it) {
std::cout << *it << std::endl;
}
}
In this case you can for example run the program as:
for %x in (*.txt) do sort.exe < %x > sorted_%x
to sort the words in all textfiles in the directory and save them with the sorted_ prefix on the filename.
If the sort program blocks at the end this would become quite a pain to do (As the user would have to press return after each file)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!