Thread: cant figure out where to put the loop....

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    38

    cant figure out where to put the loop....

    Well I finally got my basic accoutning program done, but I want it so that it doesnt quit right after it computes the info (so people dont have to use command everytime....

    I cant figure out where to put
    Code:
    while( (c=getchar()) != EOF && c != '\n' ){ }
    no matter where i think is appropriate it still just ends the program once computing is done...

    Code:
    #include <stdio.h>
    
    
    int PMR ( int input );
    int ROA ( int input );
    int WC ( int input );
    int c;
    float pmr;
    float ni;
    float ns;
    float roa;
    float avg;
    float wc;
    float ca;
    float cl;
    
    
    int main()
    {
    int input;
    
    printf( "Welcome to the Accounting Calculator v.1\n" );
    printf( "NOTE: DO NOT USE COMMAS! USE PERIOD TO PUT IN DECIMALS!\n" );
    printf( "Select from one of the following options by entering the number it corresponds to\n");
    printf( "\n");
    printf("1. Profit Margin Ratio\n");
    printf("2. Return on Assets\n");
    printf("3. Working Capital\n" );
    printf("4. Exit program\n");
    printf("Selection: ");
    scanf("%d", &input);
    switch ( input ) {
    
    	case 1:
    		 PMR( input );
    		break;
    	case 2:
    		ROA( input );
    		break;
    	case 3:
    		WC( input );
    		break;
    	case 4:
    		printf("Thanks you for using the accounting calculator\n");
    		break;
    	default:
    		printf("Bad input, quitting!\n");
    		break;
    
    	}
    }
    
    int PMR ( int input )
    {
    printf( "Enter the Net income\n" );
    scanf("%f", &ni);
    printf( "Now enter the Net sales\n" );
    scanf("%f", &ns);
    printf( "Your profit margin ratio is " );
    pmr = ni/ ns;
    pmr = pmr* 100;
    printf("%.2f", pmr);
    
    printf( "%%\n" );
    printf( "That means you are making %.2f cents off each dollar\n");
    printf( "press any button to exit the program\n" );
    while( (c=getchar()) != EOF && c != '\n' ){ }
    }
    	
    int ROA ( int input )
    {
    printf( "Enter the Net income\n" );
    scanf("%f", &ni);
    printf( "Now enter the average of total assets\n" );
    scanf("%f", &avg);
    printf( "Your Retun on assets is " );
    roa = ni/ avg;
    roa = roa* 100;
    printf("%.2f", roa);
    
    printf( "%%\n" );
    }
    	
    int WC ( int input )
    {
    printf( "Enter the Current assets\n" );
    scanf("%f", &ca);
    printf( "Now enter the Current liabilities \n" );
    scanf("%f", &cl);
    printf( "Your Working capital is " );
    wc = ca- cl;
    printf("%3.f\n", wc);
    printf( "press any button to exit the program\n" );
    getchar ();
    return 0;
    
    while( (c=getchar()) != EOF && c != '\n' ){ }
    }

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    You put it at the end of main, then follow it with a call to getchar() if you want to keep an IDE from closing the window:
    Code:
    int main()
    {
      int input;
    
      printf( "Welcome to the Accounting Calculator v.1\n" );
      printf( "NOTE: DO NOT USE COMMAS! USE PERIOD TO PUT IN DECIMALS!\n" );
      printf( "Select from one of the following options by entering the number it corresponds to\n");
      printf( "\n");
      printf("1. Profit Margin Ratio\n");
      printf("2. Return on Assets\n");
      printf("3. Working Capital\n" );
      printf("4. Exit program\n");
      printf("Selection: ");
      scanf("%d", &input);
      switch ( input ) {
      case 1:
        PMR( input );
        break;
      case 2:
        ROA( input );
        break;
      case 3:
        WC( input );
        break;
      case 4:
        printf("Thanks you for using the accounting calculator\n");
        break;
      default:
        printf("Bad input, quitting!\n");
        break;
      }
    
      while( (c=getchar()) != EOF && c != '\n' ){ }
      getchar();
    
      return 0;
    }
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    38
    awesome... got it thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  3. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  4. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM