We're just newbies in c++ so we have been doing a program called hangman where there should be 4 categories. For example : 1) c++ 2) Physics 3) Chemistry 4) History. So far we have done the hangman program but when we put it together in one program, we keep on receiving error saying " ld returned 1 exit status". Can anyone please help on how to improve our program.?
here is the wip code :
#include<iostream>
#include <cstdlib>
#include<conio.h>
#include<string.h>
#include<ctime>
using namespace std;
int num_try=8;
int checkGuess (char, string, string&);
void main_menu();
void displayrating();
string message = " Play!";
int main ()
{
char ch;
int select, select_code,ch3;
string name, phy;
char letter;
if (ch== 1 )
{
cout<<"HUNGAROO C++ EDITION!!! " << endl;
{
string physics[] = //stores the c++ terms in program
{
"string",
"void",
"algorithm",
"bool",
"char",
"library",
};
srand(time(NULL));
int n=rand()%6; //randomizes the terms
phy=physics[n];
string hide_c(phy.length(), 'X');
while(num_try!=0) //while the user still has tries left
{
main_menu();
cout<<"\n\n\t\t\t\t\t"<<hide_c;
cout<<"\n\n\t\t\tGuess a letter: ";
cin>>letter;
if(checkGuess(letter, phy, hide_c)==0)
{
message=" Incorrect letter bruh. Try again!";
num_try = num_try-1; //every wrong letter input, program minuses a try
}
else
{
message=" AWESOME! You guessed a letter";
}
if(phy==hide_c)
{
message=" Congratulations! You got it!";
main_menu();
cout<<"\n\t\t\t\tThe word is: "<<phy<<endl;
break; //ends loop after the user has guessed the city
}
if(hide_c==hide_c)
{
cout << " the letter is already there " << endl;
}
}
if(num_try==0)
{
message=" Oh My Guacamole! You've been hanged :O";
main_menu();
cout<<"\n\t\t\t\tThe word was: "<<phy<<endl;
}
displayrating();
getch();
}
cout<<"\n\nThanks for playing! See you again soon. Godbless!";
getch();
return 0;
}
{
getch(); system ( "CLS" );
cout <<" [1] Main Menu" << endl;
cout << " [2] Exit" << endl;
cin >> ch3;
if (ch3==1)
{
system ( "CLS"); return main ();
}
else if (ch3==2)
return 0;
}
srand(time(NULL));
int n=rand()%50; //randomizes the physics term city
phy=physics[n];
string hide_c(phy.length(), 'X');
while(num_try!=0) //while the user still has tries left
{
main_menu();
cout<<"\n\n\t\t\t\t\t"<<hide_c;
cout<<"\n\n\t\t\tGuess a letter: ";
cin>>letter;
if(checkGuess(letter, phy, hide_c)==0)
{
message=" Incorrect letter bruh. Try again!";
num_try = num_try-1; //every wrong letter input, program minuses a try
}
else
{
message=" AWESOME! You guessed a letter";
}
if(phy==hide_c)
{
message=" Congratulations! You got it!";
main_menu();
cout<<"\n\t\t\t\tThe word is: "<<phy<<endl;
break; //ends loop after the user has guessed the city
}
if(hide_c==hide_c)
{
cout << " the letter is already there " << endl;
}
}
if(num_try==0)
{
message=" Oh My Guacamole! You've been hanged :O";
main_menu();
cout<<"\n\t\t\t\tThe word was: "<<phy<<endl;
}
displayrating();
getch();
}
cout<<"\n\nThanks for playing! See you again soon. Godbless!";
getch();
return 0;
getch(); system ( "CLS" );
cout <<" [1] Main Menu" << endl;
cout << " [2] Exit" << endl;
cin >> ch3;
if (ch3==1)
{
system ( "CLS"); return main ();
}
else if (ch3==2)
return 0;
}
}
int checkGuess(char guess, string secretphy, string &guessphy) //checks to see if the guess matches to a letter in the city name
{
int i;
int matches=0;
int len=secretphy.length();
for(i=0; i<len; i++)
{
if(guess==guessphy[i])
return 0;
if(guess==secretphy[i])
{
guessphy[i]=guess;
matches++;
}
}
return matches;
}
void main_menu()
{
system("cls");
cout<<" ---------------------------------------------------------------------------\n";
cout<<" | How to play: Guess the word and win! Use lower case letters only |\n";
cout<<" | |\n";
cout<<" | Goodluck! |\n";
cout<<" ---------------------------------------------------------------------------\n\n";
cout<<"\t\t-----------------------------------------------";
cout<<"\n\n\t\t\t HUNGAROO PHYSICS EDITION!";
cout<<"\n\t\t\tYou have "<<num_try<<" tries to guess the terms.";
cout<<"\n\n\t\t\t"+message;
cout<<"\n\n\t\t-----------------------------------------------\n";
}
make 4 strings vectors that store the four categories
use switch() to help the user to chose which category does he want
based on the asnwer pass that string vector to the game processing part using references or pointers
for vectors and switch statement documentation google is your friend
hi, thankyou for the comment :)
we have revised the code, we only had one problem where an error says "error else without previous if" at the last part of switch statement.