Hello members!!!
I am just starting programming in C language. I had a single semester course in C about 10 years ago, when I was in college.
I apologise in advance if this is a really newb question (or) if this question has already been asked and answered(I haven't been able to find it).
Trust me, I have tried all sorts of searches using google, and on this forum and on StackOverflow but have failed to find the solution to my problem.
###########################################
###########################################
Here is my problem:
I have a programme that has multiple while loops, one for each condition. I want the loops to execute based on whether the condition is true or not.
The problem I am facing is that My code is progressing through the loops from top to bottom just fine. It is going to the next loop if the next loop's condition is being satisfied and is even repeating the current loop if the current loop's condition persists, but is not going to the previous loop and executing that loop even when the condition for the previous loop is being satisfied. The programme just quits.
P.S.
I know the same output that I want can be achieved using an 'if' loop and SubRoutines. But I want to use the while loop.
I also know that I can use 'goto' and make my programme work flawlessly.
I absolutely LOVE 'goto'.
I personally feel it's one of the best things in programming(I guess that's proof that I'm a newbie). But I've heard from most of the programmers online and even in most books, the use of goto is shunned.
So, I'm trying to get rid of my habit of using goto and I wanna know how to get my stuff to work.
Code:
void main()
{
printf("please type two variables, a and b\n\n");
int VarA=0;
int VarB=0;
scanf("%d %d", &VarA, &VarB);
getchar();
int SuM=0;
SuM=VarA+VarB;
while (SuM>25)
{
printf("The sum of numbers is greater than 25\n\n");
printf("please type two variables, a and b\n\n");
scanf("%d %d", &VarA, &VarB);
getchar();
SuM=VarA+VarB;
}
while (SuM<25)
{
printf("The sum of numbers is lesser than 25\n\n");
printf("please type two variables, a and b\n\n");
scanf("%d %d", &VarA, &VarB);
getchar();
SuM=VarA+VarB;
}
while (SuM==25)
{
printf("The sum of numbers is EQUAL to 25\n\n");
printf("please type two variables, a and b\n\n");
scanf("%d %d", &VarA, &VarB);
getchar();
SuM=VarA+VarB;
}
}
My code is working just fine if my variables are "greater than> lesser than> equal to" in that order.
However, if I assign it variables that are first lesser than 25, and then in the next cycle, assign variables whose sum is greater than 25, it fails.
It also works if my variables are such that the sum is less than 25 and then equal 25 to in that order but if i first give it 10&&15 and later in the next cycle if I give it any two variables whose sum!=25, that's it.. CRASH!!!
Please Help.