Need help with a problem

This is a discussion on Need help with a problem within the C++ Programming forums, part of the General Programming Boards category; The progrem itself is working fine ,but i want to calculed the square and cubic number without having to manually ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    7

    Need help with a problem

    The progrem itself is working fine ,but i want to calculed the square and cubic number without having to manually input the square and cibic variable(a ,c ).I tried a few ideas but every time the compuler showed and error and this is the only way I made the program work.




    Code:
    #include <cmath>
    #include <iostream>
    
    
    using namespace std;
    
    int main()
    {double a,c;
      cout<<"a=";
      cin>>a;
      cout<<"c=";
      cin>>c;
        for(int x = 1; x <= 14; x++)
    
        {if(x%2==0){
            cout<<x<<" "<<pow (x,a)<<endl;}
            else {cout<<x<<" "<<pow(x,c)<<endl; }}
        return 0;}
    Last edited by ivo93; 11-12-2012 at 02:27 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You can pass the numbers from cmd (command line) or declare them static in your program.Why is the fact the user inputs something is a problem for you?

    Also take into consideration that you have to improve your style of writing code.Take a look here Indent style - Wikipedia, the free encyclopedia

    EDIT : When i say static, i am not referring to the keyword.What i am saying is this
    Code:
    int a = 5;
    int b = 56;
    but of course in this way you should manually change the values of the variables if needed

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Thank you std10093 and thanks for the link .I'm new to c++ so any advice that will help me learn and improve is more than welcome

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are more than welcome ivo93

    A tip from me would be to clearly state why you are not happy with your code.

    A suggestion for your code would be to have an outer if that asks the user if he wants to input values.This will get you into the default values concept that you will probably meet in the future with C++.
    Example
    Code:
    #include <iostream>
    
    
    using namespace std;
    int main()
    {
        /* Initiate the variables(default values) */
        int a = 56;
        int b = 2;
        int c = 0;
    
        /* Inform the user what he should do */
        cout<<"Would you like to input? Press Y for yes and N for no"<<endl;
    
        char answer;
    
        /* Read what he wants to do */
        cin>>answer;
    
        /* Yes , he wants to input, so let him do it */
        if(answer == 'Y')
        {
            cin >> a;
            cin >> b;
            cin >> c;
        }
        /* No , he doesn't want to input.Thus default values will be left */
        else
            cout<<endl<<"Default values will be printed."<<endl<<endl;;
    
        // Print the values of the variables
        cout<<"a = "<<a<<" b = "<<b<<" c = "<<c<<endl;
    
        return 0;
    }
    Comment : Yes , again the user has to input at least once, but you will save him time if he wants no to input.Instead of three inputs he gives one.Notice that with more variables, if he didn't want to input, then again he had to input only once. This can be very helpful in programs with trees or matrices that ask the user if he wants to input something.If not , default values are preserved.

    Question : What will happen if we do not give default values to our variables and let the user decide if he wants to input or not.If he wants, then everything ok, if not, what are the values of our variables?
    In this case they will be automatically initiated to zero.But it is a good style of coding to clearly state what the values of your variables are.It also helps readability
    Last edited by std10093; 11-12-2012 at 03:15 PM.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    7
    Thanks again for another good advice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 07:45 PM
  3. Replies: 4
    Last Post: 10-16-2008, 08:30 PM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 10:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21