-
Help! Please :(
I want the user to type in a command then display a string but i keep getting my default error messege.
Code:
#include <stdlib.h>
#include <iostream>
#include <string>
#include <conio.h>
#include <windows.h>
using namespace std;
string overview();
string races();
string world();
int main()
{
char name[50];
int input;
cout << " Welcome to my test Proggy!" << endl;
cout << " What is your name?" << endl;
cin.getline(name, 50, '\n');
cout << " Hello " << name;
cout << endl << " Please choose one of the options below ";
cout << endl;
cout << " 1. Overview " << endl;
cout << " 2. Races." << endl;
cout << " 3. World." << endl;
cin >> input;
switch (input)
{
case 1: overview ();
break;
case 2: races ();
break;
case 3: world ();
break;
default:
cout<<"Error, bad input, quitting";
}
system("pause");
return 0;
}
string overview()
{
return " An Overview of DragonWars:";
}
string races()
{
return " An Overview of Races:";
}
string world()
{
return " An Overview of the world:";
}
-
Notice that your functions return a string, but there is no code to output that string. You either need to cout the string inside the function, or cout the string inside the case statement, or have a string variable in main that saves the return value of the functions and then use cout to print it after the switch statement.
-
re
Hmm....
I tried return cout << "text"; but that gave me a bunch of errors. I'm so lost :(
-
Your function can have more than one line. For example, in your function overview(), you can do:
Code:
string overview()
{
int i = 3;
cout << i;
string name = "Hi Mom!";
cout << name;
return name;
}
Notice how I did more than just return something. Don't copy that code, but make your functions cout the string you want first. Then have them return something if you want.
Also note that you don't have to have your functions return a string, especially if you don't use it. This isn't necessary to get your program to work, but if you are able to get the cout to work inside the function, then try to change the return value from string to void, since it won't make sense for your functions to return something.
Your program is fine except for this one detail. All you have to do is send the string you want to cout. No need to feel lost.
-
Thanks so much for your help, I was able to compile without any error. However when i choose the option it displays the string but imeadetly i get an error pop up of an illegal operation thne ends the program.
I've read something about return (r); Would this fix that illegal operation error?
Thanks
-
I doubt that would help but you should post all your current code if you'd really like somebody to help figure it out.
-
Oh, NM I figured it out. I declared my functions as void and removed the return values therefore it doest not cause any errors! Wow, programming is so fun lol!
-
I did? ...LOL hehe.
I solved my problem. Yay.
However a new one has arised.
I output a long string and it trails off into the right of the screen and i cant read it. I tried useing endl; but to no avail.
Code:
#include <stdlib.h>
#include <iostream>
#include <string>
#include <conio.h>
#include <windows.h>
using namespace std;
void overview();
void races();
void world();
int main()
{
char name[50];
int input;
cout << " Welcome to my test Proggy!" << endl;
cout << " What is your name?" << endl;
cin.getline(name, 50, '\n');
cout << " Hello " << name;
cout << endl << " Please choose one of the options below ";
cout << endl;
cout << " 1. Overview " << endl;
cout << " 2. Races." << endl;
cout << " 3. World." << endl;
cin >> input;
switch (input)
{
case 1: overview ();
break;
case 2: races ();
break;
case 3: world ();
break;
default:
cout<<"Error, bad input, quitting";
}
system("pause");
return 0;
}
void overview()
{
cout << " An Overview of DragonWars:" << endl;
cout << " DragonWars (Not official) is a MultiPlayer RPG where";
cout << " the player can choose between four Races: Draconians,";
cout << " Humans, Elves and Drow.";
}
void races()
{
cout << " An Overview of the races";
}
void world()
{
cout << " An Overview of the World";
}
-
You should use endl at the end of each line your writing to the console. If you don't it is put all on the same line. Putting it on several lines in the source file without using cout << endl; will not move down one line.