#### Archived

This topic is now archived and is closed to further replies.

# Whats wrong with this code... errors!!!

This topic is 5245 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

here is my code... I get 5 errors:
#include <stdio.h>
#include <conio.h>

#include <stdlib.h>
#include <time.h>

char name1[25], name2[25], name3[25];
int p1a = 0, p2a = 0, p3a = 0;
int vo = 250, won = 0, ch = 0, endturn = 0, turn = 0;

void oneTimeThings()
{
srand(time(NULL));
printf("\n");
printf("What is 1st player''s name: ");
scanf("%s", name1);
printf("\nWhat is 2nd player''s name: ");
scanf("%s", name2);
printf("\nWhat is 3rd player''s name: ");
scanf("%s", name3);
system("cls");
}

void spin()
{
int wa = (rand()%1000) /50;//Wheel amount

printf("The wheel landed on:\n");
printf("*  %d  *\n\n", wa);
}

void consonant()
{
system("cls");
printf("Not Working\n");
endturn = 1;
won = 1;
}

{
system("cls");
printf("Not Working\n");
endturn = 1;
won = 1;
}

void solvePuzzle()
{
system("cls");
printf("Not Working\n");
endturn = 1;
won = 1;
}

void freeSpin()
{
system("cls");
printf("Not Working\n");
endturn = 1;
won = 1;
}

void pass()
{
system("cls");
printf("Not Working\n");
endturn = 1;
won = 1;
}

void choice()
{
printf("What would you like to do?\n\n");
printf(" 1: Spin again\n 2: Choose a consonant\n 3: Buy a vowel\n 4: Solve the puzzle\n 5: Use a free spin\n 6: Pass\n\n");
ch=getch();
switch (ch)
{
case ''1'':
spin();
break;
case ''2'':
consonant();
break;
case ''3'':
break;
case ''4'':
solvePuzzle();
break;
case ''5'':
freeSpin();
break;
case ''6'':
pass();
break;
default:
return;
}
}

void currentPlayer()
{
char cpname[25];
if (turn == 1)
cpname = name1;
else if (turn == 2)
cpname = name2;
else if (turn == 3)
cpname = name3;
}
void playersTurn()
{
turn = 1;
switch (turn)
case ''1'':
{
printf("It is %s''s turn: \n", name1);
printf("Press a key to spin the wheel.\n\n");
getch();
spin();
do
{
choice();
}
while ( endturn == 0);
}
case ''2'':
{
printf("It is %s''s turn: \n", name1);
printf("Press a key to spin the wheel.\n\n");
getch();
spin();
do
{
choice();
}
while ( endturn == 0);
}
case ''3'':
{
printf("It is %s''s turn: \n", name1);
printf("Press a key to spin the wheel.\n\n");
getch();
spin();
do
{
choice();
}
while ( endturn == 0);
}
}

int main(void)
{
printf("Welcome to....Name that Phrase\n");
printf("Can we get the player''s names?");
oneTimeThings();
do
{
playersTurn();
}
while (won == 0);

return(0);
}

Here are the errors... C:\Documents and Settings\Matthew\My Documents\programming\ntpediting.c(105) : error C2106: ''='' : left operand must be l-value C:\Documents and Settings\Matthew\My Documents\programming\ntpediting.c(107) : error C2106: ''='' : left operand must be l-value C:\Documents and Settings\Matthew\My Documents\programming\ntpediting.c(109) : error C2106: ''='' : left operand must be l-value C:\Documents and Settings\Matthew\My Documents\programming\ntpediting.c(127) : error C2046: illegal case C:\Documents and Settings\Matthew\My Documents\programming\ntpediting.c(139) : error C2046: illegal case
(friend) WHAT!!!!..... Are you crazy??? (me) Yes I am.

##### Share on other sites
There are a few things wrong with it. First of all:

void currentPlayer(){	char cpname[25];	if (turn == 1)		cpname = name1;	else if (turn == 2)		cpname = name2;	else if (turn == 3)		cpname = name3;}

Here, you can not assign the string "name1" to the string "cpname" in this fashion. You have to use a function such as strncpy(cpname, name1, 25) etc...

And here:

void playersTurn(){	turn = 1;	switch (turn)	case ''1'':	{		printf("It is %s''s turn: \n", name1);		printf("Press a key to spin the wheel.\n\n");			getch();		spin();		do		{			choice();		}		while ( endturn == 0);	}	case ''2'':	{		printf("It is %s''s turn: \n", name1);		printf("Press a key to spin the wheel.\n\n");			getch();		spin();		do		{			choice();		}		while ( endturn == 0);	}	case ''3'':	{		printf("It is %s''s turn: \n", name1);		printf("Press a key to spin the wheel.\n\n");			getch();		spin();		do		{			choice();		}		while ( endturn == 0);	}}

You need to add braces enclosing all of the case labels (ie, a brace after switch(turn) and a closing brace after the final case statement ends).

switch(turn) {   // add this brace...case ''3'':	{		printf("It is %s''s turn: \n", name1);		printf("Press a key to spin the wheel.\n\n");			getch();		spin();		do		{			choice();		}		while ( endturn == 0);	}  }// <- And add this final brace, here

You will also want to add break statements at the end of each of the case labels in this chunk, or you will get all of them executing for the first case, etc...

Also, in that switch statement, you are switching on an int value (turn) but checking for it to be equal to the characters ''1'', ''2'', or 3. If you set it to the int values 1, 2, or 3, then it will never be equal to ''1'', ''2'', or ''3'', and none of these case statements will even execute. In this case, it will try to execute the default label; however, since there is none, you get nothing happening.

Hope this helps.

Josh
vertexnormal AT email DOT com

Check out Golem at:
My cheapass website

##### Share on other sites
Please try to limit the code you post to the few lines that seem to be causing the problem... (Admittedly in some cases that can''t be done... but this is not one of them)

http://www.cplusplus.com/doc/tutorial/tut3-2.html

and this:

http://www.cplusplus.com/doc/tutorial/tut2-1.html

http://www.cplusplus.com/doc/tutorial/tut1-2.html

##### Share on other sites
Yea I know.. the code isn''t done and I am going to get errors... This my first time using switch statements in a code... I am using them becuase a friend said they were faster then ifs

(friend) WHAT!!!!..... Are you crazy???
(me) Yes I am.

##### Share on other sites
quote:
Original post by Geoff the Medio
Please try to limit the code you post to the few lines that seem to be causing the problem... (Admittedly in some cases that can''t be done... but this is not one of them)

I personally (i dont know about anyone else) prefer to see the whole thing if its a smaller app like this.

An ASCII tetris clone... | AsciiRis

##### Share on other sites
quote:

Original post by Tiffany Smith
I personally (i dont know about anyone else) prefer to see the whole thing if its a smaller app like this.

I have to agree with Geoff here, if it is possible to narrow the problem down to a few lines it helps a lot. Many people here aren''t willing to read through 50+ lines of code, even if they can identify a problem immediately in a short fragment.

##### Share on other sites
quote:
Original post by JuNC
quote:

Original post by Tiffany Smith
I personally (i dont know about anyone else) prefer to see the whole thing if its a smaller app like this.

I have to agree with Geoff here, if it is possible to narrow the problem down to a few lines it helps a lot. Many people here aren''t willing to read through 50+ lines of code, even if they can identify a problem immediately in a short fragment.

Thats true, i never thought of that, i just like to be able to cut and paste it into my editor so that i can debug it easily and i can see any problems from there easier, but yeah i understand what you''re saying.

An ASCII tetris clone... | AsciiRis

• ### Forum Statistics

• Total Topics
628696
• Total Posts
2984265

• 18
• 9
• 13
• 13
• 11