I am a C++ beginner and I am stuck. I can't get the random number generators to run properly, specifically the random number generators are all coming up with 1 . The point system is not running properly either, I'm sure it has something to do with my do-while loop. Any insight would be appreciated.
#include<iostream>
#include<ctime>
#include<fstream>
using namespace std;
int main()
{
// Declare and initialize variables
string fileUname = " ";
string uname = " ";
int fileScore = 0;
int charaChoice = 0;
int mainChoice = 0;
int weapChoice = 0;
int score = 0;
int harry = 100;
int yoda = 200;
int gandolf = 300;
int wand = 0;
int lSaber = 0;
int magic = 0;
int weapRand = 0, i=0;
int magicRand = 0, y=0;
// Introduction
cout << "WELCOME TO TARGET PRACTICE!!!\n\n\n";
// Prompt user for name
cout << "Enter your name: ";
getline(cin,uname);
// Display main menu around a Do while loop
do{
cout<<"Please choose from the following menu\n";
cout<<"\t1) See rules\n";
cout<<"\t2) Play game\n";
cout<<"\t3) Exit\n";
cout<<"Enter choice here "<<uname<<": ";
cin>>mainChoice;
switch(mainChoice)
{
case 1:
cout<<"\tChoose 2 to start playing the game then pick a character and weapon\n\n\n";
break;
case 2:
cout<<"\tGet ready for your adventure!!!\n";
break;
case 3:
cout<<"\tThanks for playing!!!Bye\n";
return 0;
break;
default:
cout<<"Invalid choice please choose from the menu.\n";
}
}
while(mainChoice!=2);
// Display character menu
cout<<"Please choose from the following species\n";
cout<<"\t1) Harry Potter\n";
cout<<"\t2) Yoda\n";
cout<<"\t3) Gandolf\n";
cout<<"Enter choice here "<<uname<<": ";
cin>>charaChoice;
//if (charaChoice = 1){
// charaChoice = harry;
//}
//else if (charaChoice = 2){
// charaChoice = yoda;
//}
//else if (charaChoice = 3){
// charaChoice = gandolf;
//}
switch(charaChoice){
case 1:
charaChoice = harry;
break;
case 2:
charaChoice = yoda;
break;
case 3:
charaChoice = gandolf;
break;
default:
cout<< "Invalid choice choose again: ";
}
cout << charaChoice<<endl;
// Display weapon menu
do{
cout<<"Please choose from the following species\n";
cout<<"\t1) Wand\n";
cout<<"\t2) Light Saber\n";
cout<<"\t3) Magic\n";
cout<<"\t4) Try again\n";
cout<<"Enter choice here "<<uname<<": ";
cin>>weapChoice;
// if statement : if the user picks Flamethrower or Bow and arrow
// randomly generate number from 1-4 for practice session
if (weapChoice = 1){
weapChoice = wand;
srand(time(NULL)); //seed random number generator
//generate 4 numbers
for(i=1; i<=4; i++){
weapRand = rand()%4 + 1; //generate a random number from 1 to 4
}
if (weapRand = 1){
cout<< "You hit Target A, 100 points"<<endl;
charaChoice = charaChoice + 100;
}
else if (weapRand = 2){
cout<< "You hit Target B, 200 points"<<endl;
charaChoice = charaChoice + 200;
}
else if (weapRand = 3){
cout<< "You hit Target C, 300 points"<<endl;
charaChoice = charaChoice + 300;
}
else
cout<<"You missed the targets"<<endl;
cout<<weapRand<<endl;
}
if (weapChoice = 2){
weapChoice = lSaber;
srand(time(NULL)); //seed random number generator
//generate 4 numbers
{
weapRand = rand()%4 + 1; //generate a random number from 1 to 4
if (weapRand = 1){
cout<< "You hit Target A, 100 points"<<endl;
charaChoice = charaChoice + 100;
}
else if (weapRand = 2){
cout<< "You hit Target B, 200 points"<<endl;
charaChoice = charaChoice + 200;
}
else if (weapRand = 3){
cout<< "You hit Target C, 300 points"<<endl;
charaChoice = charaChoice + 300;
}
else
cout<<"You missed the targets"<<endl;
cout<<weapRand<<endl;
}
}
// if the user chooses magic user will randomly generate # number
// between 1-2 : if 1 increment 200 points if 2 decrement 100 points
if (weapChoice = 3){
srand(time(NULL)); //seed random number generator
//generate 2 numbers
}
magicRand = rand()%2 + 1; //generate a random number from 1 to 2
if (magicRand = 1){
cout<< "Your potion worked, 200 points!!"<<endl;
charaChoice = charaChoice + 200;
}
else if (magicRand = 2){
cout<< "Your potion didn't work, minus 100 points"<<endl;
charaChoice = charaChoice + 200;
}
cout<<magicRand<<endl;
weapChoice = magic;
}
while(weapChoice!=4);
cout << charaChoice<<endl;
// if score reaches below 1 or after 10 tries end game
// (outside of program) create text file "highscore.txt" containing first name and value 0
// read in highscore.txt
// if score is greater than value in file replace with users name and score else do not change the file
ifstream infile;
infile.open("highscore.txt", ios::in);
infile>>fileUname>>fileScore;
infile.close();
ofstream outfile;
if(charaChoice > fileScore)
//ofstream outfile;
outfile.open("highscore.txt", ios::out);
outfile<<uname<<" "<<charaChoice<<endl;
outfile.close();
return 0;
}