C programming, help me out!

Hitaru

pew pew
Adventurer
Joined
Jan 8, 2008
Messages
2,782
Age
39
Location
Toronto
Main Job
Puppetmaster-Male
FFXIV
Hitaru
Hey guys,

So i am in a bit of a pickle. . .My C program is being a panzy and i was just wondering if anyone could help me out with it. Here is the program:
Spoiler:

#include <stdio.h>


int getType()
{
int Bread;

printf("Welcome to Easy Bake Bread!\n");
printf("Bread Type (1-White 2-Wholewheat): ");
scanf("%d", &Bread);

while(Bread != 1 || Bread != 2){

printf("ERROR-must enter 1 or 2 for bread type!\n");
printf("Bread Type(1-White 2-Wholewheat): ");
scanf("%d", &Bread);
}

return Bread;
}

int getSize()
{
int Size;

printf("Loaf Size (1-Regular 2-Large): ");
scanf("%d", &Size);

while(Size != 1 || Size != 2){

printf(Error-Must enter 1 or 2 for Loaf Size!\n");
printf("Loaf Size (1-Regular 2-Large): ");
scanf("%d", &Size);
}


return Size;
}

int getBakeType()
{

int BType;

printf("Baking Type (1-Machine 2-Oven): ");
scanf("%d", &BType);

while(BType != 1 || BType != 2){

printf(ERROR-Must enter 1 or 2 for Baking Type!\n!");
printf("Baking Type (1-Machine 2-Oven): ");
scanf("%d", &BType);
}

return BType;
}


main()
{

int BreadType, LoafSize, BakeType;

BreadType = getType();
LoafSize = getSize();
Baketype = getBakeType();



}


I have no errors on it (although there may be some in this pasted version, i have fixed them so it can compile). Now the problem is that when it compiles the following out put happens:
Spoiler:


Welcome to Easy Bake Bread!

Bread type (1-White, 2-Wholewheat):



Now no matter what i enter into Bread type it always gives me the Error message of Must be 1 or 2. . . any help would be appreciated ; ;

If you want the Assignment details then just lemme know and i'll add em in...but if i get the functions working then it'll all be cake from there on.

Thanks for your help
 
Replace || with &&

Also, you had a lot of small mistakes in the one you posted. This is what I got when I cleaned it up a bit;

Spoiler:

#include <stdio.h>


int getType()
{
int Bread;

printf("Welcome to Easy Bake Bread!\n");
printf("Bread Type (1-White 2-Wholewheat): ");
scanf("%d", &Bread);

while(Bread != 1 && Bread != 2){

printf("ERROR-must enter 1 or 2 for bread type!\n");
printf("Bread Type(1-White 2-Wholewheat): ");
scanf("%d", &Bread);
}

return Bread;
}

int getSize()
{
int Size;

printf("Loaf Size (1-Regular 2-Large): ");
scanf("%d", &Size);

while(Size != 1 && Size != 2){

printf("Error-Must enter 1 or 2 for Loaf Size!\n");
printf("Loaf Size (1-Regular 2-Large): ");
scanf("%d", &Size);
}


return Size;
}

int getBakeType()
{

int BType;

printf("Baking Type (1-Machine 2-Oven): ");
scanf("%d", &BType);

while(BType != 1 && BType != 2){

printf("ERROR-Must enter 1 or 2 for Baking Type!\n!");
printf("Baking Type (1-Machine 2-Oven): ");
scanf("%d", &BType);
}

return BType;
}


main()
{
int BreadType, LoafSize, BakeType;

BreadType = getType();
LoafSize = getSize();
BakeType = getBakeType();



}
 
Last edited:
btw, in case you're wondering, the reason is that || means OR, while && means AND. Thus, writing || in a while loop, it constantly checks if your variable is either 1 OR 2. What you wrote means that, basically, you're skign the variable to be both 1 and 2 at once to make the condition false. By replacing OR with AND, either one can be true for the condition to show as false, and thus skipping to the next part of the program.

As a rule of thumb, try avoiding using || in combintion with != if you only use one variable. Actually, I was taught to try and avoid using that combination always, even with multiple variables and conditions, as it usually only creates problems.
 
Last edited:
btw, in case you're wondering, the reason is that || means OR, while && means AND. Thus, writing || in a while loop, it constantly checks if your variable is either 1 OR 2. What you wrote means that, basically, you're skign the variable to be both 1 and 2 at once to make the condition false. By replacing OR with AND, either one can be true for the condition to show as false, and thus skipping to the next part of the program.

As a rule of thumb, try avoiding using || in combintion with != if you only use one variable. Actually, I was taught to try and avoid using that combination always, even with multiple variables and conditions, as it usually only creates problems.

Cool thank you, I figured that bit out lol ;) the only thing left for me to do is one more function with Calculations. . .I am going to attempt it before asking for more help, thanks alot Uzor
 
Back
Top