Thread: help with c++

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    29

    Question help with c++

    Code:
    #include<stdio.h>
    int main()
    {
        char ans;
        int month,food,trans,eqp,misc;
        float total=0,over=0;
        do
        {
              printf("Enter your monthly allowance:");
              scanf("%d",&month);
              printf("Cost for food:");
              scanf("%d",&food);
              printf("Cost for transportation:");
              scanf("%d",&trans);
              printf("Cost for equipment:");
              scanf("%d",&eqp);
              printf("Other miscellaneous:");
              scanf("%d",&misc);
              total=food+trans+eqp+misc;
              printf("Total cost is RM%0.2f\n",total);
              if(total<month)
               printf("Alhamdulillah!");
              else
               over=total-month;
               printf("You exceeded RM%0.2f in your monthly allowance\n",over);
               
               printf("Enter 'y' to continue:");
               scanf("%c",&ans);
        }while(ans=='y');
        printf("Good bye.\n");
        
        system("pause");
        return 0;
    }
    what is wrong with this progm?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The big answer is "it's C, not C++".

    Other reasons are: it is -- well, not impossible, but very difficult -- for your do-while loop to loop more than once. (You would have to type the 'y' for continuing at the end of your Other miscellaneous input, since otherwise the \n character will be in the way.) If you want to make sure the input character is not whitespace (space bar, enter key, etc.) then you need to use " %c" in your scanf call.

    Other other reasons: You have to be on a OS where "pause" is a valid command to the operating system, which many people are not.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Also, I am sure that these were unintentional oversights:
    Code:
              if(total<month) {
               printf("Alhamdulillah!");
              } else {
               over=total-month;
               printf("You exceeded RM%0.2f in your monthly allowance\n",over);
              }

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Compare your program to mine, and any differences are 'wrong'.

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string _last;
        float _allowance, _food, _trans, _eqp, _misc;
        float total = 0;
    
    	cout << "Enter your monthly allowance: $";
    	cin >> _allowance;
    	cout << "Cost for food: $";
    	cin >> _food;
            cout << "Cost for transportation: $";
            cin >> _trans;
            cout << "Cost for equipment: $";
            cin >> _eqp;
            cout << "Other miscellaneous costs: $";
            cin >> _misc;
            total = _food + _trans + _eqp + _misc;
            cout << "\nTotal cost is $" << total << endl;
            
              if(total < _allowance)
                      {
    			cout << "Alhamdulillah!" << endl;
    		  }
              else if(total > _allowance)
                      {
                             cout << "You exceeded your monthly allowance! \n";
    		  }
              
             cout << "Enter \"again\" to run the program again, and press Enter to exit. \n"; 
             cin >> _last; 
             
             if(_last == "again") //Re-Execute the program if you wish.
             {
    			 main();
             }
    	else //If not, exit the program
    	{
    			 return 0;
    	 }
    
    }

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Quote Originally Posted by User-_-Name View Post
    Compare your program to mine, and any differences are 'wrong'.

    Code:
             if(_last == "again") //Re-Execute the program if you wish.
             {
    			 main();
             }
    	else //If not, exit the program
    	{
    			 return 0;
    	 }
    Well that's got to be one of the wrongest things I've ever seen.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > float _allowance, _food, _trans, _eqp, _misc;
    You should also know that identifiers beginning with underscores are reserved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Salem View Post
    > float _allowance, _food, _trans, _eqp, _misc;
    You should also know that identifiers beginning with underscores are reserved.
    To be pedantic, it's names that begin with an underscore + An uppercase letter. Or names that begins with two or more underscore identifiers. So it's actually safe in scopes inside functions (not global scope).

    Also, stdio.h should be cstdio. But then again, is it even used?

    Furthermore, remember that main shall not recursively call itself. Use a loop.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed