![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2005
Posts: 7
Rep Power: 0
![]() |
Whats wrong...
Ok I've made a program...it sucks but it works for what I want it to. Except for one thing. At the very end I have two If statements. If something ='s 1 then it tells you something and if something ='s 2 it tells you something. But for some reason if I type in 1 or 2 both of the statements appear. Any help would be appreciated. Here's the code:
#include <stdio.h> #include <conio.h> #include <stdlib.h> int main(void) { char name[10]; char name1[10]; char name2[10]; char name3[10]; char name4[10]; char name5[10]; char name6[10]; char name7[10]; char name8[10]; char name9[10]; char name10[10]; char name11[10]; char name12[10]; char name13[10]; char name14[10]; char name15[10]; char name16[10]; char name17[10]; char name18[10]; char ready[10]; int mark; printf("\tSTEWART FAMILY GIFT EXCHANGE\n\n"); //Gets all the people's names printf("Enter the 1st name.\n"); scanf("%s", name); printf("Oh ok the 1st name is %s\n", name); printf("\n\nPlease enter the 2nd name.\n"); scanf("%s", name1); printf("Oh ok the 2nd name is %s\n", name1); printf("\n\nPlease enter the 3rd name.\n"); scanf("%s", name2); printf("Oh ok the 3rd name is %s\n", name2); printf("\n\nPlease enter the 4th name.\n"); scanf("%s", name3); printf("Oh ok the 4th name is %s\n", name3); printf("\n\nPlease enter the 5th name.\n"); scanf("%s", name4); printf("Oh ok the 5th name is %s\n", name4); printf("\n\nPlease enter the 6th name.\n"); scanf("%s", name5); printf("Oh ok the 6th name is %s\n", name5); printf("\n\nPlease enter the 7th name.\n"); scanf("%s", name6); printf("Oh ok the 7th name is %s\n", name6); printf("\n\nPlease enter the 8th name.\n"); scanf("%s", name7); printf("Oh ok the 8th name is %s\n", name7); printf("\n\nPlease enter the 9th name.\n"); scanf("%s", name8); printf("Oh ok the 9th name is %s\n", name8); printf("\n\nPlease enter the 10th name.\n"); scanf("%s", name9); printf("Oh ok the 10th name is %s\n", name9); printf("\n\nPlease enter the 11th name.\n"); scanf("%s", name10); printf("Oh ok the 11th name is %s\n", name10); printf("\n\nPlease enter the 12th name.\n"); scanf("%s", name11); printf("Oh ok the 12th name is %s\n", name11); printf("\n\nPlease enter the 13th name.\n"); scanf("%s", name12); printf("Oh ok the 13th name is %s\n", name12); printf("\n\nPlease enter the 14th name.\n"); scanf("%s", name13); printf("Oh ok the 14th name is %s\n", name13); printf("\n\nPlease enter the 15th name.\n"); scanf("%s", name14); printf("Oh ok the 15th name is %s\n", name14); printf("\n\nPlease enter the 16th name.\n"); scanf("%s", name15); printf("Oh ok the 16th name is %s\n", name15); printf("\n\nPlease enter the 17th name.\n"); scanf("%s", name16); printf("Oh ok the 17th name is %s\n", name16); printf("\n\nPlease enter the 18th name.\n"); scanf("%s", name17); printf("Oh ok the 18th name is %s\n", name17); printf("\n\nOk the names will now be "); printf("distributed out"); printf("\n\nType ok when ready\n"); scanf("%s", ready); printf("\n%s gives to %s", name7, name3); printf("\n%s gives to %s", name1, name9); printf("\n%s gives to %s", name11, name5); printf("\n%s gives to %s", name4, name15); printf("\n%s gives to %s", name2, name17); printf("\n%s gives to %s", name14, name12); printf("\n%s gives to %s", name6, name13); printf("\n%s gives to %s", name10, name); printf("\n%s gives to %s", name16, name8); printf("\n%s gives to %s", name8, name14); printf("\n%s gives to %s", name15, name2); printf("\n%s gives to %s", name, name6); printf("\n%s gives to %s", name9, name11); printf("\n%s gives to %s", name12, name16); printf("\n%s gives to %s", name3, name10); printf("\n%s gives to %s", name13, name7); printf("\n%s gives to %s", name5, name1); printf("\n%s gives to %s", name17, name4); //integers are %d, characters are %s printf("\n\nPlease say what number you were, or "); printf(" press 0 to quit. "); scanf("%d",&mark); if(mark <1); { printf("\n%s gives to %s", name, name6); } if(mark == 2); { printf("\nIt Finally works!"); } return 0; } |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 5
![]() |
Im not sure if you are using c or c++. I dont really know C.
But I think your error is the semicolons following the if statements.
__________________
... |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I think the problem is huge redundancy - use arrays!
|
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Yea. Multidimensional arrays would probably help you here. Also, as Xero said, you probably don't want those semicolons after the if statements.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jan 2005
Posts: 7
Rep Power: 0
![]() |
Ok Thanks guys I took out the semicolons but it still isn't working. Can someone tell me why the arrays are better than character variables? Also if I change to arrays, do i have to change the scanf parts from %s to something else? Like %c or something? Because I know %d are for integers, and %s are for characters, so what is it for arrays?
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
an array of all your names is better so you do not have to repeat the same code 18 times...
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(void)
{
char names[18][10];
char ready[10];
int mark = 0;
int count = 0;
printf("\tSTEWART FAMILY GIFT EXCHANGE\n\n");
//Gets all the people's names
while (count < 18 ){
printf("Enter the name %d.\n", count);
scanf("%s", names[count]);
printf("Oh name %d is %s\n", count, names[count]);
count++;
}
count = 0;
printf("\n\nOk the names will now be ");
printf("distributed out");
printf("\n\nType ok when ready\n");
scanf("%s", ready);
printf("\n%s gives to %s", names[7], names[3]);
printf("\n%s gives to %s", names[1], names[9]);
printf("\n%s gives to %s", names[11], names[5]);
printf("\n%s gives to %s", names[4], names[15]);
printf("\n%s gives to %s", names[2], names[17]);
printf("\n%s gives to %s", names[14], names[12]);
printf("\n%s gives to %s", names[6], names[13]);
printf("\n%s gives to %s", names[10], names[0]);
printf("\n%s gives to %s", names[16], names[8]);
printf("\n%s gives to %s", names[8], names[14]);
printf("\n%s gives to %s", names[15], names[2]);
printf("\n%s gives to %s", names[0], names[6]);
printf("\n%s gives to %s", names[9], names[11]);
printf("\n%s gives to %s", names[12], names[16]);
printf("\n%s gives to %s", names[3], names[10]);
printf("\n%s gives to %s", names[13], names[7]);
printf("\n%s gives to %s", names[5], names[1]);
printf("\n%s gives to %s", names[17], names[4]);
//integers are %d, characters are %s
printf("\n\nPlease say what number you were, or ");
printf(" press 0 to quit. ");
scanf("%d",&mark);
if(mark <1)
{
printf("\n%s gives to %s\n", names[0], names[6]);
}
if(mark == 2)
{
printf("\nIt Finally works!\n");
}
return 0;
}I do not understand your bug with both print statements at the end.... as shown... only one of the two will print Last edited by spydoor; Feb 28th, 2005 at 2:44 PM. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jan 2005
Posts: 7
Rep Power: 0
![]() |
Wow ok thanks man! Here's the new code: (Thanks to spydoor)
#include <stdio.h> #include <conio.h> #include <stdlib.h> int main(void) { char names[18][10]; char ready[10]; int mark = 0; int count = 0; printf("\tSTEWART FAMILY GIFT EXCHANGE\n\n"); //Gets all the people's names while (count < 18 ){ printf("Enter the name %d.\n", count); scanf("%s", names[count]); printf("Oh name %d is %s\n", count, names[count]); count++; } count = 0; printf("\n\nOk the names will now be "); printf("distributed out"); printf("\n\nType ok when ready\n"); scanf("%s", ready); printf("\n%s gives to %s", names[7], names[3]); printf("\n%s gives to %s", names[1], names[9]); printf("\n%s gives to %s", names[11], names[5]); printf("\n%s gives to %s", names[4], names[15]); printf("\n%s gives to %s", names[2], names[17]); printf("\n%s gives to %s", names[14], names[12]); printf("\n%s gives to %s", names[6], names[13]); printf("\n%s gives to %s", names[10], names[0]); printf("\n%s gives to %s", names[16], names[8]); printf("\n%s gives to %s", names[8], names[14]); printf("\n%s gives to %s", names[15], names[2]); printf("\n%s gives to %s", names[0], names[6]); printf("\n%s gives to %s", names[9], names[11]); printf("\n%s gives to %s", names[12], names[16]); printf("\n%s gives to %s", names[3], names[10]); printf("\n%s gives to %s", names[13], names[7]); printf("\n%s gives to %s", names[5], names[1]); printf("\n%s gives to %s", names[17], names[4]); //integers are %d, characters are %s printf("\n\nPlease say what number you were, or "); printf(" press 0 to quit. "); scanf("%d",&mark); if(mark = 0) { printf("\n%s gives to %s\n", names[0], names[6]); } if(mark = 1) { printf("\nIt Finally works!\n"); } return 0; } Ok But the if statement at the end stil doesnt work! No Matter what number I put in, it just says "It finally works!" Any help? * I finally got it to work! I had to change the = sign to double equal sign (==) on the IF statements! Thanks for everything guys! Last edited by brandcolt; Mar 1st, 2005 at 11:49 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|