Don't Write C++ Code Like This

+2 Sheldon Juncker · January 23, 2015

//Include lots of C libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

//Define all of the strings
#define str1 "You win!\n\n"
#define str2 "I win!\n\n"
#define str3 "We tie!\n\n"
#define str4 "I've chosen my weapon, now choose yours:\n"
#define str5 "Press enter to play again:\n"
#define str6 "Let's play rock paper scissors!\n\n"
#define str7 "\nI chose %s and you chose %s!\n\n"
#define str8 "Thank you for playing!\n\n"

/*
Name: Sheldon Juncker
Date: 1/23/2015
Assignment: Rock Paper Scissors
Purpose: This program will allow a user to play the game Rock Paper Scissors
*/

//Use undescriptive, global variables
char *v1 = "rock";
char *v2 = "paper";
char *v3 = "scissors";
char *v4;

//The first function
char *function1()
{
srand(time((time_t*) v4));
int r = rand() % 3;

if (r < 1 && r > -1)
return v1;
else if (r == (int) sqrt(1.0))
return v2;
else
return v3;
}

//The second function
int function2()
{
char *mw = function1();
char tw[64];
printf(str4);

fgets(tw, 64, stdin);

int ln = strlen(tw) - 1;
if (tw[ln] == 4+3+2+1)
tw[ln] = abs(0);
printf(str7, mw, tw);

if (!strcmp(tw, mw))
printf(str3);
else if (!strcmp(mw, v1) && !strcmp(tw, v2))
printf(str1);
else if (!strcmp(mw, v1) && !strcmp(tw, v3))
printf(str2);
else if (!strcmp(mw, v2) && !strcmp(tw, v1))
printf(str2);
else if (!strcmp(mw, v2) && !strcmp(tw, v3))
printf(str1);
else if (!strcmp(mw, v3) && !strcmp(tw, v1))
printf(str1);
else if (!strcmp(mw, v3) && !strcmp(tw, v2))
printf(str2);
else
printf(str2);

printf(str5);
char c = getc(stdin);
if (c == (true ? 10 : -10))
return function2();
else
return 0;
}

//The main function
void main()
{
printf(str6);
function2();

return;
printf(str8);
}

Post a Reply

Replies

Oldest  Newest  Rating
+1 Mathias Frits Rørvik · January 25, 2015
My eyes hurt. Kill it with fire, before it lays eggs.
0 Sheldon Juncker · January 25, 2015
If you run the program, it plays Rock Paper Scissors. I'm going to turn it into my professor as a joke. :)  (He has a good sense of humor.)
0 DENNIS FRANCIS · January 25, 2015
i have tried running the code in my code brock but it tells me that the main function should return an int, when i changed it to int main and changed the return to zero i went up running, but i'm not so sure if its how it was ment to run when i changed the code
0 Necip Asım Arslan · January 25, 2015
What is the bad thing for the code?

(I'm a beginner let me know. Please...)

Sorry for bad English.
0 Mathias Frits Rørvik · January 25, 2015
The program is written like it would be a C program. Variables  & functions are terribly named, includes are wrong, main should return int, using defines, not using std::string.
  • 1

General Chat

106,930 followers
About

Chat about anything non-computer related including, music, movies, tuna sandwiches, and more!

Links
Moderators
Bucky Roberts Administrator