13 Replies - 1995 Views - Last Post: 08 October 2012 - 10:44 AM Rate Topic: -----

#1 bjm62692  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 12

Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 06:45 AM

So these are pretty basic problems, but apparently I'm doing something small wrong that is messing with my program. I'm also hoping someone can let me know if declaring my variable DN outside of the code will work like this.

This is my code
/*---------------
Include Section
----------------*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
int guess (int atmpt);
[b]int DN;[/b]

/*--------------------------- main function --------------------------------
Purpose: initiate game
Returns: prompt for random number and request to play again
----------------------------------------------------------------------------*/
int
main( ){
	char again;
	int y;
	int n;
	int atmpt;
	again = y;
	while (again == y) {
		printf("Guess a number between 1 and %i \n You have 4 guesses to find the number \n\n ",DN);
		guess(atmpt);
		printf("Would you like to play again (y/n)?\n");
		scanf("%d",&again);
	return (EXIT_SUCCESS);
}

/*--------------------------- guess function --------------------------------
Purpose: express accuracy of guesses and show when all guesses have been used up
Returns: hint or express success or failure of the game and turns left
----------------------------------------------------------------------------*/
int
guess(int atmpt){
	int counter;
	int num;
	counter = 0;
	num=rand( )%DN+1;
	while (counter <= 4);
		printf("?: \n");
		scanf("%d",&atmpt);
		if (atmpt == num)
			printf("Hooray! You win! \n");
			counter = 5;
		else if (counter == 4)
			printf("Sorry you lose. The number was: %i \n\n", num);
[b]		else
			print("Wrong number - Try again \n\n");[/b]
		if (atmpt < num)
			printf("Number is larger \n\n");
[b]		else
			printf("Number is smaller \n\n");[/b]
		counter =+ 1;
		printf("Number of guesses left is %i", 4-counter);
	while ((num = getchar( )) != EOF && num != '\n');
	return (EXIT_SUCCESS);
}



and these are my input and errors
~$ gcc [b]-DN=100[/b] game.c
game.c: In function 'main':
[u]game.c:36:3: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'char *' [-Wformat][/u]
game.c: In function 'guess':
[u]game.c:57:3: error: 'else' without a previous 'if'[/u]
game.c: In function 'main':
game.c:69:1: error: expected declaration or statement at end of input




Is This A Good Question/Topic? 0
  • +

Replies To: Wrong argument type and stubborn 'else' without previous '

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,330
  • Joined: 08-August 08

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 06:58 AM

Declaring DN "outside" main() the way you have it is declaring it to be a global variable. That will work, but you shouldn't use globals without a good reason. They make debugging difficult when the project is larger.

Oh, and when you don't use braces your if statement ends after 1 line.
This:
		if (atmpt == num)
			printf("Hooray! You win! \n");
			counter = 5;
		else if (counter == 4)
			printf("Sorry you lose. The number was: %i \n\n", num);
		else
			print("Wrong number - Try again \n\n");


reads like this:
		if (atmpt == num) {
			    printf("Hooray! You win! \n");
			}
			counter = 5; // <--- PROBLEM!
		else if (counter == 4) {
			printf("Sorry you lose. The number was: %i \n\n", num);
		} else {
			print("Wrong number - Try again \n\n");
		}


This post has been edited by CTphpnwb: 08 October 2012 - 06:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 07:10 AM

For the first error:

Quote

game.c: In function 'main':
game.c:36:3: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'char *' [-Wformat]
game.c: In function 'guess':

	char again;
    int y;
...
	again = y;
	while (again == y) {
...
		printf("Would you like to play again (y/n)?\n");
		scanf("%d",&again);

You have defined again as a character, then you try to assign the value held in an int (y) that has not been initialized to any value, then you try to compare this character to the int you never initialized, then you try to retrieve an int into a character.

I really think you should study up on variables and data types. And then the documentation for scanf().

Jim
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 07:27 AM

Consider this small program, regarding your DN on the command line.
#include <stdio.h>
int main ( ) {
    printf("Compiled with N=%d\n", N);
    return 0;
}



$ gcc -DN=100 prog.c
Was This Post Helpful? 0
  • +
  • -

#5 bjm62692  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 12

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 09:54 AM

View PostSalem_c, on 08 October 2012 - 07:27 AM, said:

Consider this small program, regarding your DN on the command line.
#include <stdio.h>
int main ( ) {
    printf("Compiled with N=%d\n", N);
    return 0;
}



$ gcc -DN=100 prog.c


So you are saying that the "D" is not part of the name of the variable?

View PostCTphpnwb, on 08 October 2012 - 06:58 AM, said:

Declaring DN "outside" main() the way you have it is declaring it to be a global variable. That will work, but you shouldn't use globals without a good reason. They make debugging difficult when the project is larger.

Oh, and when you don't use braces your if statement ends after 1 line.
This:
		if (atmpt == num)
			printf("Hooray! You win! \n");
			counter = 5;
		else if (counter == 4)
			printf("Sorry you lose. The number was: %i \n\n", num);
		else
			print("Wrong number - Try again \n\n");


reads like this:
		if (atmpt == num) {
			    printf("Hooray! You win! \n");
			}
			counter = 5; // <--- PROBLEM!
		else if (counter == 4) {
			printf("Sorry you lose. The number was: %i \n\n", num);
		} else {
			print("Wrong number - Try again \n\n");
		}



Thank you! That was a huge help!

View Postjimblumberg, on 08 October 2012 - 07:10 AM, said:

For the first error:

Quote

game.c: In function 'main':
game.c:36:3: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'char *' [-Wformat]
game.c: In function 'guess':

	char again;
    int y;
...
	again = y;
	while (again == y) {
...
		printf("Would you like to play again (y/n)?\n");
		scanf("%d",&again);

You have defined again as a character, then you try to assign the value held in an int (y) that has not been initialized to any value, then you try to compare this character to the int you never initialized, then you try to retrieve an int into a character.

I really think you should study up on variables and data types. And then the documentation for scanf().

Jim


Thank you so much!
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 09:55 AM

Correct, -D is part of the gcc command line syntax, specifically it tells the C pre-processor to pretend that
#define N 100
appeared right at the start of the source file.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 09:59 AM

It seems like you did not read the link to the scanf() documentation given to you by Jim in post #3.

The "D" (capital D) in:
-DN=100


is the option flag for "-D" which lets the user define macros at the command line. In this case, he is defining the macro N to have the value of 100.

The "d" (lowercase D) in:
printf("Compiled with N=%d\n", N);


is the type specifier in the from string's part of %d.
Was This Post Helpful? 0
  • +
  • -

#8 bjm62692  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 12

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 10:00 AM

Now I seem to be trying to impliment a function wrong. Please help.
int
main( ){
	int again;
	int y;
	int n;
	int atmpt;
	again = y;
	while (again == y) {
		printf("Guess a number between 1 and %i \nYou have 4 guesses to find the number. \n\n",N);
		guess(atmpt);
		printf("Would you like to play again (y/n)?\n");
		scanf("%d",&again);
		}
	return (EXIT_SUCCESS);
}



When the function guess works, there should be "?:" printed. However, I keep getting
Guess a number between 1 and 100 
You have 4 guesses to find the number. 

50 
30
14



Entering numbers should also give me feedback, but its not. Please help.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 10:06 AM

The semicolon at the end of line 40 in your original post is causing an infinite loop, is my best guess.

You should post your updated code to help confirm that guess.
Was This Post Helpful? 0
  • +
  • -

#10 bjm62692  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 12

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 10:14 AM

View PostSkydiver, on 08 October 2012 - 10:06 AM, said:

The semicolon at the end of line 40 in your original post is causing an infinite loop, is my best guess.

You should post your updated code to help confirm that guess.


That solved my problem. Thank you so much!
Was This Post Helpful? 0
  • +
  • -

#11 bjm62692  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-September 12

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 10:32 AM

Now I can complete it once, but once I try to answer whether or not i want to play again, it repeatedly prints the line "?: Sorry you lose. The number was: 87" as well as the random function doesn't seem to work either. the first answer is always 84.

Here is an updated look at my code.
/*---------------
Include Section
----------------*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
int guess (int atmpt);
int DN;

/*--------------------------- main function --------------------------------
Purpose: initiate game
Returns: prompt for random number and request to play again
----------------------------------------------------------------------------*/
int
main( ){
	int again;
	int y;
	int n;
	int atmpt;
	again = y;
	while (again == y) {
		printf("Guess a number between 1 and %i \nYou have 4 guesses to find the number. \n\n",N);
		guess(atmpt);
		printf("Would you like to play again (y/n)?\n");
		scanf("%d",&again);
		}
	return (EXIT_SUCCESS);
}

/*--------------------------- guess function --------------------------------
Purpose: express accuracy of guesses and show when all guesses have been used up
Returns: hint or express success or failure of the game and turns left
----------------------------------------------------------------------------*/
int
guess(int atmpt){
	int counter;
	int num;
	counter = 0;
	num=random()%N+1; //this is where i am trying to randomize the number
	while (counter <= 4) {
		printf("?: ");
		scanf("%d",&atmpt);
		if (atmpt == num) {
			printf("Hooray! You win! \n");
			counter = 5;
		}
		else if (counter == 4)
			printf("Sorry you lose. The number was: %i \n\n", num);
		else {
			printf("Wrong number - Try again \n\n");
			if (atmpt < num)
				printf("Number is larger \n\n");
			else
				printf("Number is smaller \n\n");
			counter += 1;
			printf("Number of guesses left is %i \n", 4-counter);
			}
		}
		while ((num = getchar( )) != EOF && num != '\n') //this is where i am trying to clear the random number value
		
	counter = 0;
	return (EXIT_SUCCESS);
}


Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 10:40 AM

Unless you change the random number generator seed using the srand() function, rand() will always generate the same sequence of numbers.
Was This Post Helpful? 0
  • +
  • -

#13 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 10:40 AM

18 int y;
19 int n;
20 int atmpt;
21 again = y;
22 while (again == y)
Declaring variables called y and n is NOT the same as having characters 'y' and 'n'.

Perhaps
char again = 'y';
while ( again == 'y' )


25 printf("Would you like to play again (y/n)?\n");
26 scanf("%d",&again);
Whilst %d is compatible with int again, you're not going to be typing in y or n

Following the above change, try
scanf(" %c",&again); // note the leading space - it's necessary
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,116
  • Joined: 05-May 12

Re: Wrong argument type and stubborn 'else' without previous '

Posted 08 October 2012 - 10:44 AM

On line 26, you are trying to input an integer, but your prompt on line 25 is prompting the user to enter a 'y' or an 'n' which are characters. Since the user most likely entered characters, the scanf() will fail to find any integers and those characters remain in the input buffer, and so your variable again will remain unchanged. So this will lead to the game played again, and the scanf() within your function will also find the 'y' or 'n' still in the input buffer and will also fail, so it will continue to try getting more numbers and keep on failing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1