My Final Project is suppose to include User interaction(done),a loop(done,a user defined class, write to an external file, operating overloading & at least one
Array. I just need to pass this class and I honestly never want to see a piece of code again in my life. Any help would be greatly appreciated!
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
usingnamespace std;
/*Game of Rock - Paper - Scissors*/
// Assigning variables
// Keeping track of the scores
int comp_score = 0, player_score = 0;
int comp_choice();
void winner(int comp_choice, int player_choice);
/*int main()
{
//Open file for recording game score
ifstream in_stream;
ofstream out_stream;
in_stream.open("RPS Game Score");
out_stream.open("RPS Game Score");
int first, second;
in_stream >> player_score >> comp_score;
out_stream << "The player's score is /n"
<< "The computer's score is /n"
<< endl;
in_stream.close();
out_stream.close();
return 0;
{*/
int main (void)
{
int choice;
char again = 'y';
srand(time(0));
// Intoduction to the game/do-while loop/game will continue until there is a winner.
//The the user may choose 'y' or 'Y' to continue
//or press 'N' or 'n' to quit.
do
{
cout << "Welcome to Rock, Paper, Scissors! Let's Play!\n\n";
//Output will display your score as well as the computer's.
cout << "Player - " << player_score << " | Computer - " << comp_score << "\n\n";
cout << "Type 1 for rock, 2 for paper, or 3 for scissors.\n\n";
cin >> choice;
int sec_choice = comp_choice();
winner(sec_choice, choice);
cout << "Would you like to play Again? Y/N ";
cin >> again;
system("CLS");
}
while (again == 'y' || again == 'Y');
return 0;
}
// Random number generator for the computer's choice
int comp_choice()
{
return (rand() % 3) + 1;
}
void winner(int comp_choice, int player_choice)
{
// If statement when user and computer choices are the same, output that the game was as tie
if (player_choice == comp_choice)
{
cout << "You both have chosen the same thing, you tied, try again!" << endl;
main();
}
// If-Else statements outputting computer's choice and whether the user has won or lost
if (comp_choice == 3)
cout << "The Computer has chosen scissors." << endl;
elseif(comp_choice == 2)
cout << "The Computer has chosen paper." << endl;
else
cout << "The Computer has chosen rock." << endl;
if ((player_choice == 1 && comp_choice == 3) || (player_choice == 2 && comp_choice == 1) || (player_choice == 3 && comp_choice == 2))
{
cout << "Congratulations you win!" << endl;
//This increments the player's score.
player_score+=1;
}
else
{
//This increments the computer's score.
cout << "Too bad you lose!" << endl;
comp_score+=1;
}
return;
}
As it is it works fine right now except I can't output the scores to a file & I need to incorporate a
class and an array. Is this possible for me to complete today?
Although a class is unnecessary, you could create one to manage the computer's choices, I guess. An array could be used to keep track of both scores, but again, is unnecessary.
I thought I new how to output to a file I just can't get it to work.
I have to have a class & an array to get credit.
I just need to pass this class to continue with my engineering certificate. I have
no desire to do programming after this assignment. I wouldn't have made it this
far in class without my tutor and unfortunately he is unavailable at this time.
I would appreciate any help you can give. Thanks.
You need to type the file extension when working with file I/O. There could be several files with the same name but a different file extension. Do you know how to program classes and arrays?
First of all, create a class. Declare an integer/short array to keep the scores. Declare the functions "winner()" and "comp_choice()" as methods in the class. Delete those global variables. By the way, instead of "var += 1", "var++" or "++var" can be used.
"int scores[]" should be in the class ;). You might want to make those functions static (allowing them to be accessed without an object, using the syntax "Foo::bar()" or "Foo::bar"), though.
Here is what I have so far, it is compiling but with a few errors.
I have only used a class & static function in one program. I have never used arrays.
Please tell me what else you think I am missing or doing wrong.
Thanks again.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
usingnamespace std>
/*Game of Rock - Paper - Scissors*/
class rps
{
public:
int scores[];
void winner();
int comp_choice90;
};
// Assigning variables
// Keeping track of the scores
int comp_score = 0, player_score = 0;
void winner(int comp_choice, int player_choice);
int main (void)
{
ofstream out_stream;
out_stream.open ("RPS.txt");
out_stream << "Thank You for playing Rock Paper Scissors.\n";
out_stream.close();
int choice;
char again = 'y';
srand(time(0));
// Intoduction to the game/do-while loop/game will continue until there is a winner.
//The the user may choose 'y' or 'Y' to continue
//or press 'N' or 'n' to quit.
do
{
cout << "Welcome to Rock, Paper, Scissors! Let's Play!\n\n";
//Output will display your score as well as the computer's.
cout << "Player - " << player_score << " | Computer - " << comp_score << "\n\n";
cout << "Type 1 for rock, 2 for paper, or 3 for scissors.\n\n";
cin >> choice;
int sec_choice = comp_choice();
winner(sec_choice, choice);
cout << "Would you like to play Again? Y/N ";
cin >> again;
system("CLS");
}
while (again == 'y' || again == 'Y');
return 0;
}
// Random number generator for the computer's choice
int comp_choice()
{
return (rand() % 3) + 1;
}
void winner(int comp_choice, int player_choice)
{
// If statement when user and computer choices are the same, output that the game was as tie
if (player_choice == comp_choice)
{
cout << "You both have chosen the same thing, you tied, try again!" << endl;
main();
}
// If-Else statements outputting computer's choice and whether the user has won or lost
if (comp_choice == 3)
cout << "The Computer has chosen scissors." << endl;
elseif(comp_choice == 2)
cout << "The Computer has chosen paper." << endl;
else
cout << "The Computer has chosen rock." << endl;
if ((player_choice == 1 && comp_choice == 3) || (player_choice == 2 && comp_choice == 1) || (player_choice == 3 && comp_choice == 2))
{
cout << "Congratulations you win!" << endl;
//This increments the player's score.
player_score++;
}
else
{
//This increments the computer's score.
cout << "Too bad you lose!" << endl;
comp_score++;
return;
}
Well, for one thing, functions usually aren't declared with a suffix of "90". Which compiler do you use? It'd be much easier for you if you'd read the errors you get and read the line the error report points to, because that way you'd be able to solve some errors on your own. Or, at the very least, copy/paste me the reports along with the line they point to.
1>Build started 12/11/2011 2:07:23 PM.
1>InitializeBuildStatus:
1> Touching "Debug\Final C++ Project.unsuccessfulbuild".
1>ClCompile:
1> Final.cpp
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(19): warning C4200: nonstandard extension used : zero-sized array in struct/union
1> Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(21): error C2229: class 'rps' has an illegal zero-sized array
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(43): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(60): error C3861: 'comp_choice': identifier not found
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(72): error C2561: 'main' : function must return a value
1> c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(32) : see declaration of 'main'
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(123): fatal error C1075: end of file found before the left brace '{' at 'c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(83)' was matched
1>
1>Build FAILED.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
usingnamespace std;
/*Game of Rock - Paper - Scissors*/
class rps
{
public:
int scores[];
void winner();
int comp_choice();
};
// Assigning variables
// Keeping track of the scores
int comp_score = 0, player_score = 0;
void winner(int comp_choice, int player_choice);
int main (void)
{
ofstream out_stream;
out_stream.open ("RPS.txt");
out_stream << "Thank You for playing Rock Paper Scissors.\n";
out_stream.close();
int choice;
char again = 'y';
srand(time(0));
// Intoduction to the game/do-while loop/game will continue until there is a winner.
//The the user may choose 'y' or 'Y' to continue
//or press 'N' or 'n' to quit.
do
{
cout << "Welcome to Rock, Paper, Scissors! Let's Play!\n\n";
//Output will display your score as well as the computer's.
cout << "Player - " << player_score << " | Computer - " << comp_score << "\n\n";
cout << "Type 1 for rock, 2 for paper, or 3 for scissors.\n\n";
cin >> choice;
int sec_choice = comp_choice();
winner(sec_choice, choice);
cout << "Would you like to play Again? Y/N ";
cin >> again;
system("CLS");
}
while (again == 'y' || again == 'Y');
return 0;
}
// Random number generator for the computer's choice
int comp_choice()
{
return (rand() % 3) + 1;
}
void winner(int comp_choice, int player_choice)
{
// If statement when user and computer choices are the same, output that the game was as tie
if (player_choice == comp_choice)
{
cout << "You both have chosen the same thing, you tied, try again!" << endl;
main();
}
// If-Else statements outputting computer's choice and whether the user has won or lost
if (comp_choice == 3)
cout << "The Computer has chosen scissors." << endl;
elseif(comp_choice == 2)
cout << "The Computer has chosen paper." << endl;
else
cout << "The Computer has chosen rock." << endl;
if ((player_choice == 1 && comp_choice == 3) || (player_choice == 2 && comp_choice == 1) || (player_choice == 3 && comp_choice == 2))
{
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1
1>Build started 12/11/2011 2:39:51 PM.
1>InitializeBuildStatus:
1> Touching "Debug\Final C++ Project.unsuccessfulbuild".
1>ClCompile:
1> Final.cpp
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(19): warning C4200: nonstandard extension used : zero-sized array in struct/union
1> Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(43): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(60): error C3861: 'comp_choice': identifier not found
1>c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(123): fatal error C1075: end of file found before the left brace '{' at 'c:\users\jasen\desktop\c++\final project\final project\final project\final.cpp(83)' was matched
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.16
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
According to the error reports, you need to give the array a size ahead of time, type "return 0" instead of "return" at the end of main() and add another '}' at the end.
Oh, I just noticed, you called main() as a function. Don't do that, that causes stack overflow.
EDIT: You wrote "using namespace std>", but for some reason you don't have any errors from that. It should be "using namespace std;".
All of my code didn't show up last time sorry.
I gave the array a size of 10.
When I put a 0 at the end of my last return statement I get an error.
Do I get rid of main() altogether?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
usingnamespace std;
/*Game of Rock - Paper - Scissors*/
class rps
{
public:
int scores[10];
void winner();
int comp_choice();
};
// Assigning variables
// Keeping track of the scores
int comp_score = 0, player_score = 0;
void winner(int comp_choice, int player_choice);
int main (void)
{
ofstream out_stream;
out_stream.open ("RPS.txt");
out_stream << "Thank You for playing Rock Paper Scissors.\n";
out_stream.close();
int choice;
char again = 'y';
srand(time(0));
// Intoduction to the game/do-while loop/game will continue until there is a winner.
//The the user may choose 'y' or 'Y' to continue
//or press 'N' or 'n' to quit.
do
{
cout << "Welcome to Rock, Paper, Scissors! Let's Play!\n\n";
//Output will display your score as well as the computer's.
cout << "Player - " << player_score << " | Computer - " << comp_score << "\n\n";
cout << "Type 1 for rock, 2 for paper, or 3 for scissors.\n\n";
cin >> choice;
int sec_choice = comp_choice();
winner(sec_choice, choice);
cout << "Would you like to play Again? Y/N ";
cin >> again;
system("CLS");
}
while (again == 'y' || again == 'Y');
return 0;
}
// Random number generator for the computer's choice
int comp_choice()
{
return (rand() % 3) + 1;
}
void winner(int comp_choice, int player_choice)
{
// If statement when user and computer choices are the same, output that the game was as tie
if (player_choice == comp_choice)
{
cout << "You both have chosen the same thing, you tied, try again!" << endl;
main();
}
// If-Else statements outputting computer's choice and whether the user has won or lost
if (comp_choice == 3)
cout << "The Computer has chosen scissors." << endl;
elseif(comp_choice == 2)
cout << "The Computer has chosen paper." << endl;
else
cout << "The Computer has chosen rock." << endl;
if ((player_choice == 1 && comp_choice == 3) || (player_choice == 2 && comp_choice == 1) || (player_choice == 3 && comp_choice == 2))
{
cout << "Congratulations you win!" << endl;
//This increments the player's score.
player_score++;
}
else
{
//This increments the computer's score.
cout << "Too bad you lose!" << endl;
comp_score++;
return 0;
}
}