ClioSport.net

Register a free account today to become a member!
Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

  • When you purchase through links on our site, we may earn an affiliate commission. Read more here.

C languge question



  Fiat Panda 100hp
We have been set a programming assignment at college, and I have created the program, but it has to be handed in next week.

And my mate has just pointed out that my languge doesnt look like C and more like C something else.

We have turbo C on the computers at college that we are suppost to use to create the program using C, but I have done most of it at home using the micosoft C compiler.

Is there any difference between the 2 languges? can I hand it in?

Thanks
 

sn00p

ClioSport Club Member
  A blue one.
They'll both either be ansi C89 or C90 (same thing - different standards organsiation codes) providing you've not used C++.

Lets see your code!
 
  Fiat Panda 100hp
[c]#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//windows.h for color
#include <windows.h>

//globals...
char * data[13];
int answers[13] = { 0, 4, 2, 4, 1, 3, 3, 2, 4, 4, 2, 5, 4 };
bool set[13];

//globals
HANDLE hConsole;
const int COLOR_RED = 12;
const int COLOR_GREEN = 10;
const int COLOR_BLUE = 1;
const int COLOR_YELLOW = 14;
const int COLOR_WHITE = 15;
const int COLOR_FORE = 15;
const int COLOR_BACK = 8;

/*
* Colors:
* forecolor + (backcolor * 16) = full color setup
* 0 is black
*/

//function prototypes
void loadData();
void prepData();
void showData(int line);
void printHR();
void printHR(int width);
void setColors(int fore, int back);
void setColors(int colorValue);

//main function - entry point
void main()
{
//get handle to the console
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

srand(time(NULL));
loadData();

//setup colors to start
setColors(COLOR_FORE, COLOR_BLUE);
//setColors(115);
system("cls");

printf("\n");
printf("\tWelcome to the game!\n\tTo play, choose the one that doesn't belong.\n");

bool quit = false;
bool ingame = false;
while (!quit)
{
printf("\n");
printf("\t==Main Menu==\n");
printf("\t1. Play Game\n");
printf("\t2. Quit\n");

//print hr
printHR();

char menuChoice;
printf("\tYour choice: ");

//set colors
setColors(COLOR_YELLOW, COLOR_BLUE);
//cin >> menuChoice;
scanf("%s", &menuChoice);
//set colors
setColors(COLOR_FORE, COLOR_BLUE);

switch(menuChoice)
{
case '1':
system("cls");
ingame = true;
break;
case '2':
system("cls");
ingame = false;
quit = true;
break;
default:
system("cls");
printf("\n\tPlease make a valid choice!\n");
ingame = false;
break;
}

printf("\n");

int completed = 0;
int totalQuestions = 12;
int correct = 0;
bool gameCompleted = false;

prepData();

char initials[256];
bool initialsFlag = false;

if (ingame)
{
printf("\n");
printf("\tPlease enter your initails: ");

//set colors
setColors(COLOR_YELLOW, COLOR_BLUE);

//get the user's initials (or anything up to 256 chars)
//cin.ignore(1, '\n');
//cin.getline(initials,256);
scanf("%256s", &initials);
printf("\n");

//reset colors
setColors(COLOR_FORE, COLOR_BLUE);

initialsFlag = true;
}

while (ingame)
{
if (!initialsFlag)
{
printf("\n");
printf("\tPlease enter your initails: ");
//cin >> initials;
scanf("%256s", &initials);
printf("\n");

initialsFlag = true;
}

if (completed >= totalQuestions)
{
gameCompleted = true;
ingame = false;
break;
}

int qnum = 0;
bool validQuestion = false;

while (!validQuestion)
{
qnum = rand()%12 + 1; //1-12
if (set[qnum])
continue;
else
validQuestion = true;
}

set[qnum] = true;

printf("\n");
showData(qnum);
char answerChoice;

bool ready = false;
while (!ready)
{
printf("\tQ = Quit.\n");

//print HR
printHR();

printf("\tYour choice: ");

//set colors
setColors(COLOR_YELLOW, COLOR_BLUE);

//cin >> answerChoice;
scanf("%s", &answerChoice);

//reset colors
setColors(COLOR_FORE, COLOR_BLUE);

int a = (int)answerChoice - 48;

if (a == (int)'q' - 48)
{
ready = true;
ingame = false;
system("cls");
}
else if (a < 1 || a > 5)
{
//system("cls");
//cout << endl;
//print HR
printHR();

//set colors
setColors(COLOR_RED, COLOR_BLUE);
printf("\tInvalid Choice!\n\n");
//set colors
setColors(COLOR_FORE, COLOR_BLUE);
}
else if (a == answers[qnum])
{
//system("cls");
//cout << endl;
//print HR
printHR();

//set colors
setColors(COLOR_GREEN, COLOR_BLUE);
printf("\tCorrect!\n\n");
//set colors
setColors(COLOR_FORE, COLOR_BLUE);

correct++;
completed++;
ready = true;
printf("\t");
system("PAUSE");
system("cls");
}
else
{
//system("cls");
//cout << endl;
//print HR
printHR();

//set colors
setColors(COLOR_RED, COLOR_BLUE);
printf("\tIncorrect! Nice try!\n\n");
//set colors
setColors(COLOR_FORE, COLOR_BLUE);

completed++;
ready = true;
printf("\t");
system("PAUSE");
system("cls");
}
}
}

if (gameCompleted)
{
system("cls");
double percentCorrect = ((double)correct / (double)totalQuestions) * 100;
printf("\n");
if (!initialsFlag)
printf("\tCongratulations on completing the game!\n");
else
printf("\tCongratulations on completing the game %s!\n", initials);

//print HR
printHR();

//set colors
setColors(COLOR_GREEN, COLOR_BLUE);
printf("\tYou got %d out of %d correct!", correct, totalQuestions);
printf("\n");
printf("\tThat is %d Percent!\n", (int)percentCorrect);
//set colors
setColors(COLOR_FORE, COLOR_BLUE);

printHR();
printf("\t");
system("PAUSE");
system("cls");
}
}
}

//load data
void loadData()
{
for (int i = 1; i < 13; i++)
{
data = (char*)malloc(65);
}

data[1]="1 rose, 2 daffodil, 3 tulip, 4 cauliflower, 5 carnation";
data[2]="1 hen, 2 hare, 3 duck, 4 goose, 5 turkey";
data[3]="1 beech, 2 elm, 3 oak, 4 violet, 5 ash";
data[4]="1 slate, 2 gold, 3 silver, 4 iron, 5 lead";
data[5]="1 potato, 2 turnip, 3 lemon, 4 carrot, 5 beetroot";
data[6]="1 oats, 2 wheat, 3 scones, 4 barley, 5 rye";
data[7]="1 salmon, 2 whale, 3 herring, 4 mackerel, 5 cod";
data[8]="1 man, 2 boy, 3 master, 4 princess, 5 uncle";
data[9]="1 shield, 2 lance, 3 dagger, 4 gun, 5 spear";
data[10]="1 orange, 2 potato, 3 cherry, 4 apple, 5 banana";
data[11]="1 cottage, 2 mansion, 3 palace, 4 bungalow, 5 warehouse";
data[12]="1 red, 2 blue, 3 yellow, 4 skin, 5 green";
}

void prepData()
{
for (int i = 0; i < 13; i++)
{
set = false;
}
}

//show data
void showData(int line)
{
printf("\tQuestion: %s \n", data[line]);
}

//print a horizontal rule - default = 50
void printHR()
{
printf("\t__________________________________________________\n\n");
}

void printHR(int width)
{
printf("\t");
for (int i = 0; i < width; i++)
{
printf("_");
}
printf("\n\n");
}

//change the console colors
void setColors(int fore, int back)
{
SetConsoleTextAttribute(hConsole, (fore + (back * 16)));
//must put a system("cls") after this because
//colors only change fully if the screen is cleared.
//if no cls, then only the text and its direct
//background are affected. calling cls does the whole
//console.
}

void setColors(int colorValue)
{
SetConsoleTextAttribute(hConsole, colorValue);
}[/c]
 

sn00p

ClioSport Club Member
  A blue one.
Your friend is saying that because you've used windows specific functions. (console colouring)

If the turbo 'C' you're using isn't a windows version then it won't compile or link on it because it wont have the windows headers or libraries.
 
  Fiat Panda 100hp
Your friend is saying that because you've used windows specific functions. (console colouring)

If the turbo 'C' you're using isn't a windows version then it won't compile or link on it because it wont have the windows headers or libraries.

Ar yes, but it is C just compiled using a microsoft compliler?
 

sn00p

ClioSport Club Member
  A blue one.
Your friend is saying that because you've used windows specific functions. (console colouring)

If the turbo 'C' you're using isn't a windows version then it won't compile or link on it because it wont have the windows headers or libraries.

Ar yes, but it is C just compiled using a microsoft compliler?

Yeah, it is just 'C'....

But you've included a non-standard header (windows.h), if the compiler that they try to compile on (dos turbo c?) doesn't have this header, then it won't compile.
 

sn00p

ClioSport Club Member
  A blue one.
Just some observations... ;)

Why have you not kept your arrays zero indexed? You can initialise your data array at the top of your program like, instead of your malloc loop and then assigning the string pointers in code.

Code:
char *data[] = 
{
	"1 rose, 2 daffodil, 3 tulip, 4 cauliflower, 5 carnation",
	"1 hen, 2 hare, 3 duck, 4 goose, 5 turkey"
};

Which leads me onto question number 2! Why not create a structure like:

Code:
typedef struct 
{
	char *question;
	int answer;
} QUESTION_DATA;

and initialise the data like:

Code:
QUESTION_DATA data[] =
{
	{"1 rose, 2 daffodil, 3 tulip, 4 cauliflower, 5 carnation", 4},
	{"1 hen, 2 hare, 3 duck, 4 goose, 5 turkey", 2}
};

for what it's worth. Unless you've not got to structures on your course yet!
 
  Fiat Panda 100hp
Lol, it could do the above, but as it is done now and this is the only assignment on it it will not be changed. I dont plan to do near C again after this, hate it.

Thanks for the pointers though Snoop.
 

sn00p

ClioSport Club Member
  A blue one.
Lol, it could do the above, but as it is done now and this is the only assignment on it it will not be changed. I dont plan to do near C again after this, hate it.

Thanks for the pointers though Snoop.

'C' makes the world go round. Literally! ;)
 


Top