Loop Problems....

Please see this code and tell me where is some thing wrong

Page 1 of 1

7 Replies - 913 Views - Last Post: 25 August 2008 - 08:03 PM Rate Topic: -----

#1 aqshaf  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 18-August 08

Loop Problems....

Posted 19 August 2008 - 10:53 PM

//This program is using while loop with sentinel values
#include<stdio.h>
int main()
{
    float salary, commission, sales;
    sales = 0;
    commission = 0;
    salary = 0;
    int sal = 200;
    while( sales != -1 ) // loop starting depending on conditon
    {
    printf(" \n \n Please enter your sales. \n");
    scanf( "%f", &sales);
    commission = sales * 0.09;
    salary = sal + commission;
    printf( "\n Your salary without commmission is: %d", sal);
    printf( "\n Your salaray with commission is: %.2f", salary);
    printf( "\n \n \n \n Thanks very much, take care bye...cordially \n \n \n \t");
}
return 0; // successful ending
}
//Remarks: Fail (this program is not working properly....


*edit: Fixed code tags :code:

This post has been edited by Martyr2: 19 August 2008 - 10:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Loop Problems....

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Loop Problems....

Posted 19 August 2008 - 11:02 PM

it helps if you tell us what the expected behavior is, and what is actually happening.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: Loop Problems....

Posted 19 August 2008 - 11:11 PM

Be sure to prompt for a value prior to going into a sentinel loop. That way you can make sure everything is good and if the value is bad (like they want to escape) that the loop contents are never ran.

We also prompt again at the end of the loop to start the next while loop iteration.

Also make sure to initialize your float values properly.

Lastly, thank them after the work has been done and they have chosen to quit the program.

Here is the code that should help you out...

//This program is using while loop with sentinel values
#include<stdio.h>

int main()
{
     float salary, commission, sales;

     // Remember to initialize variables to proper values
     // the letter "f" tells the compiler to treat them as floats.

     sales = 0.00f;
     commission = 0.00f;
     salary = 0.00f;
     int sal = 200;

     // This is called "priming the loop"
     // We prompt for a value prior to going into our loop
     // We also let them know the escape value (in this case being -1)
     printf(" \n \n Please enter your sales or -1 to quit. \n");
     scanf( "%f", &sales);

     while( sales != -1.00 ) // loop starting depending on conditon
     {
		commission = sales * 0.09f;
		salary = sal + commission;
		printf( "\n Your salary without commmission is: %d", sal);
		printf( "\n Your salary with commission is: %.2f", salary);

		// Notice we reprompt at the end of the loop.
		printf(" \n \n Please enter your sales or -1 to quit. \n");
		scanf( "%f", &sales);
     }

     // We thank them after they have chosen to exit the program only.
     printf( "\n \n \n \n Thanks very much, take care bye...cordially \n \n \n \t");

     return 0; // successful ending
}



Read through the in-code comments to see what I have done and fixed up. Then everything should be working fine for you.

Enjoy!

"At DIC we be while loop fixing code ninjas... we also fix games, races, and your two front teeth" :snap:
Was This Post Helpful? 1
  • +
  • -

#4 aqshaf  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 18-August 08

Re: Loop Problems....

Posted 19 August 2008 - 11:58 PM

View PostMartyr2, on 19 Aug, 2008 - 11:11 PM, said:

Be sure to prompt for a value prior to going into a sentinel loop. That way you can make sure everything is good and if the value is bad (like they want to escape) that the loop contents are never ran.

We also prompt again at the end of the loop to start the next while loop iteration.

Also make sure to initialize your float values properly.

Lastly, thank them after the work has been done and they have chosen to quit the program.

Here is the code that should help you out...

//This program is using while loop with sentinel values
#include<stdio.h>

int main()
{
     float salary, commission, sales;

     // Remember to initialize variables to proper values
     // the letter "f" tells the compiler to treat them as floats.

     sales = 0.00f;
     commission = 0.00f;
     salary = 0.00f;
     int sal = 200;

     // This is called "priming the loop"
     // We prompt for a value prior to going into our loop
     // We also let them know the escape value (in this case being -1)
     printf(" \n \n Please enter your sales or -1 to quit. \n");
     scanf( "%f", &sales);

     while( sales != -1.00 ) // loop starting depending on conditon
     {
		commission = sales * 0.09f;
		salary = sal + commission;
		printf( "\n Your salary without commmission is: %d", sal);
		printf( "\n Your salary with commission is: %.2f", salary);

		// Notice we reprompt at the end of the loop.
		printf(" \n \n Please enter your sales or -1 to quit. \n");
		scanf( "%f", &sales);
     }

     // We thank them after they have chosen to exit the program only.
     printf( "\n \n \n \n Thanks very much, take care bye...cordially \n \n \n \t");

     return 0; // successful ending
}



Read through the in-code comments to see what I have done and fixed up. Then everything should be working fine for you.

Enjoy!

"At DIC we be while loop fixing code ninjas... we also fix games, races, and your two front teeth" :snap:

oooooooooooooo thnks very much...............gr8 gr8.............................May God Bless you
Was This Post Helpful? 0
  • +
  • -

#5 aqshaf  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 18-August 08

Re: Loop Problems....

Posted 22 August 2008 - 09:01 AM

Please see this code
this is working properly but in the result is showing zero "0".....mean no interest
please see where is problem thanks
/* Example 3.20
In this example I will calculate the Interest rate on a said amount
depending upon tenure and its rate.
I will use while loop with a sentinel value.....in major..*/
#include <stdio.h>

int main ()
{
// declarations and initializations of variables
float rate, interest, principal, days;
days = 0.00f; //here I use all variables as float data type
principal = 0.00f;
rate = 0.00f;
interest = 0.00f;
printf( "\nPlease enter the Principal or -1 to quit:\t");
scanf( "\n%f", &principal);
printf( "\nPlease enter the days: \t");
scanf( "%d", &days);
printf( "\nPlease enter the interest rate: \t");
scanf( "%f", &rate);
while ( principal != -1 )
{
interest = principal * rate * days/365;
printf( "\nThe interest rate on \"%.3f \" is: \t %.3f \n", principal, interest);
//Reprompting again
printf( "\nPlease enter the Principal or -1 to quit:\t");
scanf( "\n%f", &principal);
}
printf( "\n\nThanks very much, Take care.\n");
return 0;
}
Was This Post Helpful? 0
  • +
  • -

#6 aqshaf  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 18-August 08

Re: Loop Problems....

Posted 25 August 2008 - 01:11 AM

want this output
3.......5......7.......9
6.......8.....10.....12
9.......11...13.....15
12.....14...16.....18
15.....17....19....21


but see my program this is showing some thing wrong !!!!!
// example 3.26
#include<stdio.h>
#include<conio.h>
int main()
{
int i = 1, j = 1;
while ( i <= 5 )
{ //starting of while loop
int a = 3;
printf( "%d\n", a);
while ( j <= 4 )
{ // starting of 1st while loop

int b = a ;
printf( "\t%d", b = b + 2);
j++;
} // ending of 1st while loop
a = a + 3;
i++;
} //ending of 2nd loop of while
printf( "\n\n\t\tTake care");
getch ();
return 0;
} //ending of main
Was This Post Helpful? 0
  • +
  • -

#7 red_4900  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,120
  • Joined: 22-February 08

Re: Loop Problems....

Posted 25 August 2008 - 01:47 AM

isn't it is easier to do that using nested for loop? like this :

#include<stdio.h>
int main(void){
int a=3,i,j;

for(i=1;i<=5;i++){
	for(j=1;j<=4;j++){
		printf("%d\t",a);
		a = a + 2;
		}
	printf("\n");
	a = 3+(3*i);
	}

printf("Take care");

return 0;
}


one advice, getch() function from conio.h is platform dependant, try not to use it. :)
Was This Post Helpful? 0
  • +
  • -

#8 aqshaf  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 18-August 08

Re: Loop Problems....

Posted 25 August 2008 - 08:03 PM

Dear Thanks very much, May you always be happy.....Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1