» Search Used Cars
Search for used vehicles by ZIP, please enter Zipcode below:
Google Links

» Wheel & Tire Center

» Log in
User Name:

Password:

Not a member yet?
Register Now!
Sponsors

Sponsors


Go Back   NissanForums.com :: Nissan Forum > General > Off Topic
Register Home Forum Gallery Active Topics Search Today's Posts Mark Forums Read

       
Reply
 
LinkBack Thread Tools Display Modes
Old Feb 29th, 2004, 08:07 PM   #1 (permalink)
Ninety-Nine SE-L
Please Shift Here
 
Ninety-Nine SE-L's Avatar
 
Join Date: May 2002
Location: Orlando, FL
Posts: 6,694
Send a message via AIM to Ninety-Nine SE-L
I need someone who knows C-programming

PLEASE HELP. just simple thing, I don't care if the program works perfect. can anyone point out any errors and try to get my program to compile? It's an assignment.

sorry, the forums doesn't support tabs, if you want it in txt format, PM or email me.

/*title*/

/* Program is to simulate a casino game*/

#include <stdio.h>
#include <stdlib.h>

void mainmenu(void);
int pairofdice(void); /*c represents chips, k represents cash*/
int craps(int c);
int arupsdice(int c);
int buychips(int k);
int sellchips(int c);
int sellallchips(int c);
void statusreport(int c, int k);



int main (void) {

int cash=1000, numchips=0, change_in_cash=0;
int menuchoice, loop=1;

while (loop == 1){
mainmenu();
scanf("%d", &menuchoice);
printf("\n");

if (menuchoice <= 6 && menuchoice >=1){
if (menuchoice == 1){
change_in_cash = buychips(cash);
cash -= change_in_cash;
numchips += (change_in_cash/11);
change_in_cash = 0;
}
if else (menuchoice == 2){
change_in_cash = sellchips(numchips);
cash += change_in_cash;
numchips -= (change_in_cash/10);
change_in_cash = 0;
}
if else (menuchoice == 3){
numchips = craps(numchips);
}
if else (menuchoice == 4){
numchips = arupsdice(numchips);
}
if else (menuchoice == 5){
statusreport(numchips, cash);
}
if else (menuchoice == 6){
change_in_cash = sellallchips(numchips);
cash += change_in_cash;

printf("Thank you for playing Casino.\nYou have finished with a total of $%d.\n", cash);
loop = 0;
}
}
else {
printf("Not a valid choice.");
}
}

return 0;
}


void mainmenu (void) {

printf("Welcome to the Casino. Please make a selection\n");
printf("1)Buy chipe\n2)Sell chips\n3)Play Craps\n4)Play Arup's Game of Dice\n5)Status Report\n6)Quit\n");

}


int pairofdice(void) {

int total, a, b;
a = 1+rand()%6;
b = 1+rand()%6;

total = a+b;

return total;
}


int craps(int chips) {

int chips_won, total_chips, how_many, dice, k, pop=1;

printf("You have %d chips\n", chips);
printf("How many chips would you like to bet? ");
scanf("%d", &how_many);
printf("\n");

if (how_many > chips){
printf("NOT ENOUGH CHIPS!!! No game played\n");
return 0;
}
else {
dice = pairofdice();
printf("You rolled a %d.\n", dice);

if (dice == 7 || dice==11){
printf("You Win!!! %d chips have been added to your total.\n", how_many);
total_chips = chips + how_many;
}
if else (dice == 2 || dice == 3 || dice == 12){
printf("You Lose. %d chips have been deducted from your total.\n", how_many);
total_chips = chips - how_many;
}
else {
while (pop==1){
k = pairofdice();
if (k == dice){
printf("You Win!!! %d chips have been added to your total.\n", how_many);
total_chips = chips + how_many;
pop = 0;
}
if else (k == 7){
printf("You Lose. %d chips have been deducted from your total.\n", how_many);
total_chips = chips - how_many;
pop = 0;
}
}

}
}
return total_chips;
}


int arupsdice(int chips) {

int chips_won, total_chips, how_many, dice, k;

printf("You have %d chips\n", chips);
printf("How many chips would you like to bet? ");
scanf("%d", &how_many);
printf("\n");

if (how_many > chips){
printf("NOT ENOUGH CHIPS!!! No game played\n");
return 0;
}
else {
dice = pairofdice();
printf("You rolled a %d.\n", dice);

if (dice == 12 || dice == 11){
printf("You Win!!! %d chips have been added to your total.\n", how_many);
total_chips = chips + how_many;
}
if else (dice == 2){
printf("You Lose. %d chips have been deducted from your total.\n", how_many);
total_chips = chips - how_many;
}
else {
k = pairofdice();
if (k > dice){
printf("You Win!!! %d chips have been added to your total.\n", how_many);
total_chips = chips + how_many;
}
else{
printf("You Lose. %d chips have been deducted from your total.\n", how_many);
total_chips = chips - how_many;
}

}
}
return total_chips;
}


int buychips(int cash) {

int how_many, cash_required;

printf("You have $%d\n", cash);
printf("How many chips would you like to buy? ");
scanf("%d", &how_many);
printf("\n");
cash_required = (how_many*11);

if (cash_required > cash){
printf("NOT ENOUGH CASH!!!\n");
return 0;
}
else {
return cash_required;
}
}


int sellchips(int chips) {

int how_many, cash_made;

printf("You have %d chips\n", chips);
printf("How many chips would you like to sell? ");
scanf("%d", &how_many);
printf("\n");
cash_made = (how_many*10);

if (how_many > chips){
printf("NOT ENOUGH CHIPS!!!\n");
return 0;
}
else {
return cash_made;
}
}


int sellallchips(int chips) {

int cash_made;
cash_made = (chips*10);

return cash_made;

}


void statusreport(int chips, int cash) {

printf("Status: You currently have %d chips and $%d.", chips, cash);
}

Last edited by Ninety-Nine SE-L : Feb 29th, 2004 at 08:25 PM.
Ninety-Nine SE-L is offline   Reply With Quote
Sponsored Links
Advertisement
 
Old Feb 29th, 2004, 08:21 PM   #2 (permalink)
Ninety-Nine SE-L
Please Shift Here
 
Ninety-Nine SE-L's Avatar
 
Join Date: May 2002
Location: Orlando, FL
Posts: 6,694
Send a message via AIM to Ninety-Nine SE-L
actually, just help me out with this. I debugged as much as I could, I keep getting "parse error" and I don't know what that means.

program3.c: In function `main':
program3.c:41: parse error before "else"
program3.c:47: parse error before "else"
program3.c:50: parse error before "else"
program3.c: At top level:
program3.c:60: parse error before string constant
program3.c:60: warning: conflicting types for built-in function `printf'
program3.c:60: warning: data definition has no type or storage class
program3.c:61: warning: data definition has no type or storage class
program3.c:62: parse error before '}' token
program3.c: In function `craps':
program3.c:114: parse error before "else"
program3.c:118: parse error before "else"
program3.c:126: parse error before "else"
program3.c: At top level:
program3.c:131: parse error before '}' token
program3.c: In function `arupsdice':
program3.c:160: parse error before "else"
program3.c:164: parse error before "else"
program3.c: At top level:
program3.c:176: parse error before '}' token
Ninety-Nine SE-L is offline   Reply With Quote
Old Feb 29th, 2004, 08:34 PM   #3 (permalink)
muccman
Nissan Fanatic
 
Join Date: Aug 2003
Location: Canada
Posts: 274
i knew comp sci would pay off

/* Program is to simulate a casino game*/

#include <stdio.h>
#include <stdlib.h>

void mainmenu(void);
int pairofdice(void); /*c represents chips, k represents cash*/
int craps(int c);
int arupsdice(int c);
int buychips(int k);
int sellchips(int c);
int sellallchips(int c);
void statusreport(int c, int k);



int main (void) {

int cash=1000, numchips=0, change_in_cash=0;
int menuchoice, loop=1;

while (loop == 1){
mainmenu();
scanf("%d", &menuchoice);
printf("\n");

if (menuchoice <= 6 && menuchoice >=1){
if (menuchoice == 1){
change_in_cash = buychips(cash);
cash -= change_in_cash;
numchips += (change_in_cash/11);
change_in_cash = 0;
}

// you put an if else here
if (menuchoice == 2){
change_in_cash = sellchips(numchips);
cash += change_in_cash;
numchips -= (change_in_cash/10);
change_in_cash = 0;
}
//here
if (menuchoice == 3){
numchips = craps(numchips);
}
//here
if (menuchoice == 4){
numchips = arupsdice(numchips);
}
//here
if (menuchoice == 5){
statusreport(numchips, cash);
}
// here again..
if (menuchoice == 6){
change_in_cash = sellallchips(numchips);
cash += change_in_cash;

printf("Thank you for playing Casino.\nYou have finished with a total of $%d.", cash);
loop = 0;
}
}
else {
printf("Not a valid choice.");
}
}

return 0;
}


void mainmenu (void) {

printf("Welcome to the Casino. Please make a selection\n");
printf("1)Buy chipe\n2)Sell chips\n3)Play Craps\n4)Play Arup's Game of Dice\n5)Status Report\n6)Quit\n");

}


int pairofdice(void) {

int total, a, b;
a = 1+rand()%6;
b = 1+rand()%6;

total = a+b;

return total;
}


int craps(int chips) {

int chips_won, total_chips, how_many, dice, k, pop=1;

printf("You have %d chips\n", chips);
printf("How many chips would you like to bet? ");
scanf("%d", &how_many);
printf("\n");

if (how_many > chips){
printf("NOT ENOUGH CHIPS!!! No game played\n");
return 0;
}
else {
dice = pairofdice();
printf("You rolled a %d.\n", dice);

if (dice == 7 || dice==11){
printf("You Win!!! %d chips have been added to your total.\n", how_many);
//how many is not an int.. cant have a space:P
total_chips = chips + how_many;
}
// its either if or else, not both
if (dice == 2 || dice == 3 || dice == 12){
printf("You Lose. %d chips have been deducted from your total.\n", how_many);
//same here
total_chips = chips - how_many;
}
else {
while (pop==1){
k = pairofdice(); // forgot a semi colon
if (k == dice){
printf("You Win!!! %d chips have been added to your total.\n", how_many);
//jeez man.. again
total_chips = chips + how_many;
pop = 0;
}

// what is it with you and if elses?
if (k == 7){
printf("You Lose. %d chips have been deducted from your total.\n", how_many);
// how many instead of how_many
total_chips = chips - how_many;
pop = 0;
}
}

}
}
return total_chips;
}


int arupsdice(int chips) {

int chips_won, total_chips, how_many, dice, k;

printf("You have %d chips\n", chips);
printf("How many chips would you like to bet? ");
scanf("%d", &how_many);
printf("\n");

if (how_many > chips){
printf("NOT ENOUGH CHIPS!!! No game played\n");
return 0;
}
else {
dice = pairofdice();
printf("You rolled a %d.\n", dice);

if (dice == 12 || dice == 11){
printf("You Win!!! %d chips have been added to your total.\n", how_many);
total_chips = chips + how_many;//how many again
}
// if else.. pick one or the other
if (dice == 2){
printf("You Lose. %d chips have been deducted from your total.\n", how_many);
total_chips = chips - how_many; // how many.. yet again
}
else {
k = pairofdice(); // semi colon
if (k > dice){
printf("You Win!!! %d chips have been added to your total.\n", how_many);
total_chips = chips + how_many;// how many again again
}
else{
printf("You Lose. %d chips have been deducted from your total.\n", how_many);
total_chips = chips - how_many; // yeah.. you guessed it!
}

}
}
return total_chips;
}


int buychips(int cash) {

int how_many, cash_required; // not a function, dont need the ( at the end

printf("You have $%d\n", cash);
printf("How many chips would you like to buy? ");
scanf("%d", &how_many);
printf("\n");
cash_required = (how_many*11);

if (cash_required > cash){
printf("NOT ENOUGH CASH!!!\n");
return 0;
}
else {
return cash_required;
}
}


int sellchips(int chips) {

int how_many, cash_made; // not a function, dont need ( at the end

printf("You have %d chips\n", chips);
printf("How many chips would you like to sell? ");
scanf("%d", &how_many);
printf("\n");
cash_made = (how_many*10);

if (how_many > chips){
printf("NOT ENOUGH CHIPS!!!\n");
return 0;
}
else {
return cash_made;
}
}


int sellallchips(int chips) {

int cash_made;
cash_made = (chips*10);

return cash_made;

}


void statusreport(int chips, int cash) {

printf("Status: You currently have %d chips and $%d.", chips, cash);
}






try that.. it worked for me on C++.. your biggest problems were when you put if else.. you put and if OR an else.. not both... how many is not a variable.. how_many is... you forgot a semi colon here and there.. and put some ) in some places... didnt take too much.. i put comments wherever i changed something..


Marc
__________________
1992 Nissan Sentra Classic 5spd

Last edited by muccman : Feb 29th, 2004 at 09:01 PM.
muccman is offline   Reply With Quote
Old Feb 29th, 2004, 08:36 PM   #4 (permalink)
Ninety-Nine SE-L
Please Shift Here
 
Ninety-Nine SE-L's Avatar
 
Join Date: May 2002
Location: Orlando, FL
Posts: 6,694
Send a message via AIM to Ninety-Nine SE-L
ya, I forgot about the _ in the how many, I fixed those. Lemme try compiling in my compiler
Ninety-Nine SE-L is offline   Reply With Quote
Old Feb 29th, 2004, 08:48 PM   #5 (permalink)
Ninety-Nine SE-L
Please Shift Here
 
Ninety-Nine SE-L's Avatar
 
Join Date: May 2002
Location: Orlando, FL
Posts: 6,694
Send a message via AIM to Ninety-Nine SE-L
OMG, I got everything to work, thank you. I thought 'if else' was a statement.

Anyway, you're pretty useful, I'm gonna remember you .
Ninety-Nine SE-L is offline   Reply With Quote
Sponsored Links
Advertisement
 
Old Feb 29th, 2004, 08:52 PM   #6 (permalink)
OPIUM
All over your face!
 
OPIUM's Avatar
 
Join Date: Aug 2002
Posts: 2,390
that stuff makes my head spin. Reminds me of my javascripting days. I should have kept up on it, cause now I'm sitting here scratching my head trying to figure out some dhtml
OPIUM is offline   Reply With Quote
Old Feb 29th, 2004, 09:00 PM   #7 (permalink)
muccman
Nissan Fanatic
 
Join Date: Aug 2003
Location: Canada
Posts: 274
that's a fairly simple program too.. if it was spaced out i probably could have understood it pretty fast.. i jsut used the compiler and went error by error.. a wee bit of sloppy coding but that's about it... gotta love being in grade 12 computer science
__________________
1992 Nissan Sentra Classic 5spd
muccman is offline   Reply With Quote
Old Feb 29th, 2004, 09:04 PM   #8 (permalink)
dry
NF's Offical Post Whore
 
dry's Avatar
 
Join Date: Oct 2002
Location: MTown/PBurgh, N.Y
Posts: 1,972
Send a message via ICQ to dry Send a message via AIM to dry Send a message via MSN to dry Send a message via Yahoo to dry
Thumbs up

post #3500
dry is offline   Reply With Quote
Old Feb 29th, 2004, 09:08 PM   #9 (permalink)
Ninety-Nine SE-L
Please Shift Here
 
Ninety-Nine SE-L's Avatar
 
Join Date: May 2002
Location: Orlando, FL
Posts: 6,694
Send a message via AIM to Ninety-Nine SE-L
I actually wrote it very neat, Nissanforums jus doesn't support tabbing, so it all seems to start with the same indent. It is a pretty simple program, it's only programming assignment 3 of 6 in my programming class. I knew a bit of it before I entered the class, I jus hate doing all that (but it's funny, I don't seem to mind html )
Ninety-Nine SE-L is offline   Reply With Quote
Old Mar 1st, 2004, 06:04 AM   #10 (permalink)
Scott
Post Freak
 
Join Date: Apr 2002
Posts: 1,075
Quote:
Originally Posted by 1997 GA16DE
I actually wrote it very neat, Nissanforums jus doesn't support tabbing, so it all seems to start with the same indent.
Use the code tags.
Scott is offline   Reply With Quote
Sponsored Links
Advertisement
 
Old Mar 1st, 2004, 08:11 AM   #11 (permalink)
Ninety-Nine SE-L
Please Shift Here
 
Ninety-Nine SE-L's Avatar
 
Join Date: May 2002
Location: Orlando, FL
Posts: 6,694
Send a message via AIM to Ninety-Nine SE-L
o ya, forgot that.
Ninety-Nine SE-L is offline   Reply With Quote
Old Mar 1st, 2004, 08:13 AM   #12 (permalink)
Ninety-Nine SE-L
Please Shift Here
 
Ninety-Nine SE-L's Avatar
 
Join Date: May 2002
Location: Orlando, FL
Posts: 6,694
Send a message via AIM to Ninety-Nine SE-L
schweet
Code:
/*title*/

/* Program is to simulate a casino game*/

#include <stdio.h>
#include <stdlib.h>

void mainmenu(void);
int pairofdice(void); /*c represents chips, k represents cash*/
int craps(int c);
int arupsdice(int c);
int buychips(int k); 
int sellchips(int c);
int sellallchips(int c);
void statusreport(int c, int k);



int main (void) {
	
	int cash=1000, numchips=0, change_in_cash=0;
	int menuchoice, loop=1;

	while (loop == 1){
		mainmenu();
		scanf("%d", &menuchoice);
		printf("\n");
	
		if (menuchoice <= 6 && menuchoice >=1){
			if (menuchoice == 1){
				change_in_cash = buychips(cash);
				cash -= change_in_cash;
				numchips += (change_in_cash/11);
				change_in_cash = 0;
			}
			if (menuchoice == 2){
				change_in_cash = sellchips(numchips);
				cash += change_in_cash;
				numchips -= (change_in_cash/10);
				change_in_cash = 0;
			}
			if (menuchoice == 3){
				numchips = craps(numchips);
			}
			if (menuchoice == 4){
				numchips = arupsdice(numchips);
			}
			if (menuchoice == 5){
				statusreport(numchips, cash);
			}
			if (menuchoice == 6){
				change_in_cash = sellallchips(numchips);
				cash += change_in_cash;
				
				printf("Thank you for playing Casino.\nYou have finished with a total of $%d.\n", cash);
				loop = 0;
			}
		}
		else {
			printf("Not a valid choice.");
		}
	}

	return 0;
}


void mainmenu (void)	{

	printf("Welcome to the Casino.  Please make a selection\n");
	printf("1)Buy chips\n2)Sell chips\n3)Play Craps\n4)Play Arup's Game of Dice\n5)Status Report\n6)Quit\n");

}


int pairofdice(void) {

	int total, a, b;
	a = 1+rand()%6;
	b = 1+rand()%6;
	
	total = a+b;
	
	return total;
}


int craps(int chips) {

	int chips_won, total_chips, how_many, dice, k, pop=1;
	
	printf("You have %d chips\n", chips);
	printf("How many chips would you like to bet?  ");
	scanf("%d", &how_many);
	printf("\n");
	
	if (how_many > chips){
		printf("NOT ENOUGH CHIPS!!!  No game played\n");
		return 0;
	}
	else {
		dice = pairofdice();
		printf("You rolled a %d.\n", dice);
		
		if (dice == 7 || dice==11){
			printf("You Win!!!  %d chips have been added to your total.\n", how_many);
			total_chips = chips + how_many;
		}
		if (dice == 2 || dice == 3 || dice == 12){
			printf("You Lose.  %d chips have been deducted from your total.\n", how_many);
			total_chips = chips - how_many;
		}
		else {
			while (pop==1){
				k = pairofdice();
				if (k == dice){
					printf("You Win!!!  %d chips have been added to your total.\n", how_many);
					total_chips = chips + how_many;
					pop = 0;
				}
				if (k == 7){
					printf("You Lose.  %d chips have been deducted from your total.\n", how_many);
					total_chips = chips - how_many;
					pop = 0;
				}
			}
			
		}
	}
	return total_chips;
}


int arupsdice(int chips) {

	int chips_won, total_chips, how_many, dice, k;
	
	printf("You have %d chips\n", chips);
	printf("How many chips would you like to bet?  ");
	scanf("%d", &how_many);
	printf("\n");
	
	if (how_many > chips){
		printf("NOT ENOUGH CHIPS!!!  No game played\n");
		return 0;
	}
	else {
		dice = pairofdice();
		printf("You rolled a %d.\n", dice);
		
		if (dice == 12 || dice == 11){
			printf("You Win!!!  %d chips have been added to your total.\n", how_many);
			total_chips = chips + how_many;
		}
		if (dice == 2){
			printf("You Lose.  %d chips have been deducted from your total.\n", how_many);
			total_chips = chips - how_many;
		}
		else {
			k = pairofdice();
			if (k > dice){
				printf("You Win!!!  %d chips have been added to your total.\n", how_many);
				total_chips = chips + how_many;
			}
			else{
				printf("You Lose.  %d chips have been deducted from your total.\n", how_many);
				total_chips = chips - how_many;
			}
			
		}
	}
	return total_chips;
}


int buychips(int cash) {

	int how_many, cash_required;
	
	printf("You have $%d\n", cash);
	printf("How many chips would you like to buy?  ");
	scanf("%d", &how_many);
	printf("\n");
	cash_required = (how_many*11);
	
	if (cash_required > cash){
		printf("NOT ENOUGH CASH!!!\n");
		return 0;
	}
	else {
		return cash_required;
	}
}


int sellchips(int chips) {

	int how_many, cash_made;
	
	printf("You have %d chips\n", chips);
	printf("How many chips would you like to sell?  ");
	scanf("%d", &how_many);
	printf("\n");
	cash_made = (how_many*10);
	
	if (how_many > chips){
		printf("NOT ENOUGH CHIPS!!!\n");
		return 0;
	}
	else {
		return cash_made;
	}
}


int sellallchips(int chips) {
	
	int cash_made;
	cash_made = (chips*10);
	
	return cash_made;
	
}


void statusreport(int chips, int cash) {

	printf("Status: You currently have %d chips and $%d.\n", chips, cash);
}
Ninety-Nine SE-L is offline   Reply With Quote
Old Mar 26th, 2004, 09:11 PM   #13 (permalink)
Ninety-Nine SE-L
Please Shift Here
 
Ninety-Nine SE-L's Avatar
 
Join Date: May 2002
Location: Orlando, FL
Posts: 6,694
Send a message via AIM to Ninety-Nine SE-L
hey again, I need help once more, this is a much easier program involving arrays, I can't get it to pop out of the second for loop, someone run it and tell me what's wrong. It compiles just fine.

Instructions:
enter in how many classes there are (lets say 3),
enter in how many students for class #1,
enter in all the grades for class #1,
averages all the grades for class #1, then finds how many are above average,
enter in how many students for class #2,
enter in all the grades for class #2,
averages all the grades for class #2, then finds how many are above average,
enter in how many students for class #3,
enter in all the grades for class #3,
averages all the grades for class #3, then finds how many are above average,

then it displays in this format:
Class 1 had 12 students, an average of 65 and 5 grades above average.
Class 2 had 6 students, an average of 71 and 3 grades above average.
Class 3...

Here's what I wrote:
Code:
/*
  March 26, 2004
  Program 4
*/

/* Program is supposed to read in grades for a given number of classes
   and output the averages for each class*/

#include <stdio.h>
#include <stdlib.h>

int main (void) {
	
	int classes, students[50], grades[50], averages[50], above[50];
	int i, j, k, x, y, z;
	
	printf("How many classes?\n");
	scanf("%d", &classes);
	printf("\n");
	
	for (i = 1; i <= classes; ++i){
		printf("How many students in class %d?\n", i);
		scanf("%d", &students[i]);
		printf("\n");
		
		printf("Input grades\n");
		for (j = 1; j <= students[i]; ++j){
			scanf("%d", &grades[j]);
			printf ("\n");
			x += grades[j];
		}

		y = x / students[i]; /*find average*/
		averages[i] = y; /*read average into array*/
		k = 0;
		for (j = 1; j <= students[i]; ++j){
			while (grades[j] > averages[i]){
				k += 1;
			}
			averages[i] = k;
		}
		printf("\n");
	}
	
	for (j = 1; j <= classes; ++j){
		printf("Class %d had %d students, an average of %d and %d grades above average.\n", j, students[j], averages[j], above[j]);
	}
	return 0;
}
Thanks for any help.
Ninety-Nine SE-L is offline   Reply With Quote
Reply

  NissanForums.com :: Nissan Forum > General > Off Topic