-
lessthen or equal to
im almost finished with my home work. i just need to know is there is a way for me to do less then or equal to function for an 'if' statement.
you lot probably understand what i mean but here's an example anyway...
Code:
if (Votes_Needed<=State_Two_Votes)
{
printf("If %s can win %s then %s will win the election\n",Candidate, State_Two, Candidate);
system("pause");
return 0;
}
i was hoping that the '<= 'sign would work...it didn't =(
and my help book on my programs sucks!
-
Well, that would work, if Votes_Needed and State_Two_Votes were say, int variables.
-
If one or both are functions, you need to called the function with parentheses: ().
Post the exact error you're getting -- it really helps people tell what is happening.
-
ok im sorry i assumed that it was the sign that made the program crash. my program did compile but after the inputs it crashed. (windows error "program needs to close")
here is the code for the hole thing...i though it was good =/
Code:
//John Torres
//COP 3223-0002
//Assignment #1.3
//September, 4, 2008
#include <stdio.h>
int main(void)
{
char State_One[21];
char State_Two[21];
char Candidate[21];
int State_One_Votes;
int State_Two_Votes;
int Both_States;
int Votes_Won;
int Votes_Needed;
//input
printf("Who is your candiate?\n(Obama or McCain for 2008)\n");
scanf("%s",Candidate);
printf("What is the name of the first state in question?\n");
scanf("%s",&State_One);
printf("What is the name of the second state?\n");
scanf("%s",&State_Two);
printf("How many votes are in %s?\n",State_One);
scanf("%d",&State_One_Votes);
printf("How many votes are in %s?\n",State_Two);
scanf("%d",&State_Two_Votes);
printf("How many votes does %s already have?\n",Candidate);
scanf("%d",Votes_Won);
//math
Votes_Needed=270-Votes_Won;
Both_States=State_One_Votes+State_Two_Votes;
//outputs
if (Votes_Needed<1)
{
printf("%s has already won! Go party!\n",Candidate);
system("pause");
return 0;
}
if (Votes_Needed<=State_One_Votes)
{
if (Votes_Needed>State_Two_Votes)
{
printf("If %s can win %s then %s will win the election\n",Candidate, State_One, Candidate);
system("pause");
return 0;
}
if (Votes_Needed<=State_Two_Votes)
{
printf("If %s can win %s or %s then %s will win the election\n",Candidate, State_One, State_Two, Candidate);
system("pause");
return 0;
}
}
if (Votes_Needed<=State_Two_Votes)
{
printf("If %s can win %s then %s will win the election\n",Candidate, State_Two, Candidate);
system("pause");
return 0;
}
if (Votes_Needed<=Both_States)
{
printf("If %s can win %s and %s then %s will win the election\n",Candidate, State_One, State_Two, Candidate);
system("pause");
return 0;
}
if (Votes_Needed>Both_States)
{
printf("%s cannot win.\n",Candidate);
system("pause");
return 0;
}
}
-
scanf needs you to use & if the thing you are reading into is NOT a pointer. Since State_One, State_Two, and Candidate are given to scanf as pointers (pointing the front of the array), then you do NOT need & here. Since Votes_Won is NOT a pointer, you need &.
-
You forgot something here:
Code:
printf("How many votes does %s already have?\n",Candidate);
scanf("%d",&Votes_Won);
-
...
i cant believe i missed that. sorry i wasted your time. =/
-
-
and i was so happy when it compiled my first try too.
anyway it works and does what i want now.
Thanx alot!
-
as an aside, if you had used an incorrect symbol you would have received a compiler error.
-
-
You have another problem: http://cboard.cprogramming.com/showp...37&postcount=9
And use getchar() instead of system("pause") is you can. The latter is unportable.