if statement not working

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
Prophet][
DCEmu Mega Fan
DCEmu Mega Fan
Posts: 2984
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Fri May 31, 2002 6:10 am
Location: Adelaide
Has thanked: 0
Been thanked: 0

if statement not working

Post by Prophet][ »

ok, I've been learning C++ today. I don't have a book on it yet, I only have 10 powerpoint slides I printed off the uni web site. If you have read the post I made a few days ago I made bout how I got screwed over by the uni, you'll understand my mood. oh, And I'm tired

Ok, now my code isn't working. It complies but it only does the first line in the for loop. heres the code

Code: Select all


#include <stdio.h>

int count;
int i;
char answer;


int main()
{

	printf("File: prac2ex2.c\n");
	printf("Aurthor: \n");
	printf("Student id: \n");
	printf("Email id: \n");
	printf("******************************************\n");
	printf("This is my own work as defined by the\n");
	printf("University's Academic Misconduct Policy.\n");
	printf("\n");

	count = 100;

	printf("Would you like to sing a song?");
	scanf("%d",&answer);
	printf("\n");
	if (answer = 'y')
	{
	     printf("How many verses of the song do you wish to sing?");
	     scanf("%d", &i);
	     printf("\n");
	     i = 100 - i;

	     while(i < count)
	      {
		printf("%d bottles of beer on the wall\n", i);
		printf("%d bottles of beer\n", i);
		printf("If one of those bottles should happen to fall\n");
		i--;
		printf("%d bottles of beer on the wall\n", i);
		printf("\n");
	       }
	
	}
	 
	else
	     printf("");

		return 0;

} // end main
If you say you want to sing a song, it then asks you how many verses but then ends. I need it to at least get to the while loop. What a, I doing wrong?
GPF
DC Developer
DC Developer
Posts: 529
Joined: Wed Oct 17, 2001 7:44 pm
Location: Texas
Has thanked: 0
Been thanked: 0
Contact:

Post by GPF »

scanf("%d",&answer);
printf("\n");
if (answer = 'y')

Should be

Code: Select all

scanf("%c",&answer); 
   printf("\n"); 
   if (answer == 'y')
scanf parameter is for a character (%c), not an number (%d) , I don't know if its a C/C++ issue there or just my compiler doesn't like converting explicitly from integer to char

also your if statement was an assignment ( = ) not an equality ( == )

Troy
User avatar
Prophet][
DCEmu Mega Fan
DCEmu Mega Fan
Posts: 2984
Joined: Fri May 31, 2002 6:10 am
Location: Adelaide
Has thanked: 0
Been thanked: 0

Post by Prophet][ »

[edit] don't worry bout this [/edit]
Last edited by Prophet][ on Sun Mar 14, 2004 7:16 am, edited 1 time in total.
User avatar
Prophet][
DCEmu Mega Fan
DCEmu Mega Fan
Posts: 2984
Joined: Fri May 31, 2002 6:10 am
Location: Adelaide
Has thanked: 0
Been thanked: 0

Post by Prophet][ »

ahh, I get it now
speud
DCEmu Uncool Newbie
DCEmu Uncool Newbie
Posts: 1459
Joined: Sat Dec 27, 2003 10:40 pm
Has thanked: 0
Been thanked: 0
Contact:

Post by speud »

also i think it should be i++ not i--, i mean to do the good number of loops, not to display the correct dicreasing value
http://blueswirl.fr.st - DC Online Tools and Downloads

thx to Wack0 for the avatar ;)
User avatar
Prophet][
DCEmu Mega Fan
DCEmu Mega Fan
Posts: 2984
Joined: Fri May 31, 2002 6:10 am
Location: Adelaide
Has thanked: 0
Been thanked: 0

Post by Prophet][ »

yea, I found that out aftereards, and I just changed all the i's in the while loop to count.
Derived Class
DCEmu Crazy Poster
DCEmu Crazy Poster
Posts: 35
Joined: Mon Mar 01, 2004 1:27 pm
Has thanked: 0
Been thanked: 0
Contact:

Post by Derived Class »

GPF wrote:
scanf("%d",&answer);
printf("\n");
if (answer = 'y')

Should be

Code: Select all

scanf("%c",&answer); 
   printf("\n"); 
   if (answer == 'y')
scanf parameter is for a character (%c), not an number (%d) , I don't know if its a C/C++ issue there or just my compiler doesn't like converting explicitly from integer to char

also your if statement was an assignment ( = ) not an equality ( == )

Troy
Ive been working through some C examples where the args to scanf send integer types... no problems
Derived Class - Geek, Idiot, Heartbreaker

Xbox GT - Derived Class
Xbox, Dreamcast Gamer

Derived Class Is A Geek
Post Reply