|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
I am very new to programming, and have been trying to figure out this problem for over an hour now. Can some one help me figure out what is wrong with my code?
This is a simple version of a funtion I am working on. I want it to print "Unscramble the word: rnaooagk." Then check the players response against the answer. The code compiles fine. But when I run it, nothing is printed to the screen. My other question is: Is it possible to setup my funtion with values for 2 strings? (ex.) string question(string word, string answer); -This is my first time posting a quesion. I hope I made my problem clear.- --------------------------------- Code:
#include
#include
using namespace std;
string reply;
string answer = "kangaroo";
string word;
string question(string word);
int main()
{
string question("rnaooagk");
cin.get();
}
string question(string word)
{
do
{
cout << "Unscramble the word: " << word;
cin >> reply;
} while (reply != answer);
return reply;
}
Last edited by WingedPanther; 09-14-2008 at 01:58 PM. Reason: add code tags |
| Sponsored Links |
|
|
|
|||
|
I think I got it. -code posted below-
If you know of a way to better write this piece of code, please share it with me. Thank you. -------------------------------------- Code:
#include <iostream>
#include <string>
using namespace std;
string reply;
string answer; // kangaroo rnaooagk
string word;
string word2;
string question(string word, string answer);
int main()
{
string word2 = question("rnaooagk", "kangaroo");
cin.get();
}
string question(string word, string answer)
{
do
{
cout <<"Unscramble the word: "<< word << "\n";
cin >> reply;
} while (reply != answer);
return reply;
}
Last edited by WingedPanther; 09-14-2008 at 01:59 PM. Reason: add code tags |
|
|||||
|
Technically, your variable declarations should be inside your main/question function (as appropriate). As they are now, they are global variables, and passing them to the function doesn't do anything useful.
Also, please use code tags when writing code (you can press the # icon to get them).
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
![]() |
| Tags |
| c++, function, prototype, string |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |