What the heck is wrong with this scrips

This is a discussion on What the heck is wrong with this scrips within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { int N1; char act; int N2; cout ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    21

    Exclamation What the heck is wrong with this scrips

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        int N1;
        char act;
        int N2;
    
    
        cout << "\t\t|~-~-~-~-~-~-~|\n"
                "\t\t| Calculator  |\n"
                "\t\t|~-~-~-~-~-~-~|\n";
        cout << "\n"
                "+=Add\t\t\t S=sqrt\n"
                "-=Subtract\t\t ^=Exponent\n"
                "*=multiply\t\t COMEING\n"
                "/=divide\t\t Q=Quit\n\n";
        while(act != 'Q')
            {
                cin>>N1>>act>>N2;
                if (act='+')
                    cout<<N1+N2;
                if (act='-')
                    cout<<N1-N2;
                if (act='*')
                    cout<<N1*N2;
                if (act='/')
                    cout<<N1/N2;
                if (act='s')
                    cout<<sqrt(N1);
                if (act='^')
                    cout<<pow(N1, N2);
            }
        cin.get();
    }

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    I am just a beginner myself but I see at least one problem and that is that you are not initializing your variables when you declare them meaning they could hold any value.

    Next to that it looks to me like a weird place to put a while loop because you are not looping. Try a conditional branch instead like "if".
    Code:
    if( act != 'Q')
    {
    // Your code
    }
    Last edited by Crux; 02-07-2011 at 12:41 PM.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    21

    I want it to loop there because,

    I want it to loop there so that you can continue entering different equations in without re-opening. Without a loop u can only enter 1 eqaution each time u open the program.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    Well I am not an expert like I said but it would be nice if you mentioned what is going wrong. Does it not compile are there any run time problems?

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    21

    ok

    OH, yeah srry. Well when I try to quit it gets a glitchy and puts tons of numbers(infinate 0's and 1's sometimes and +[some random large number] over and over again). I just made the script go on forever to make it work. To quit u just push button at top. So my next problem is it always things I want to add the numbers together. If I put 76*13 it says 89, or 5-2 it says 7...

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    Well like I said I am a beginner myself and I might be overlooking the same thing as you are but what I think you could try the characters in a 'char'. I do not know if they are reserved by C++ or not. You might want to try it using the ASCII value of the character instead.

    Did you also tried it with initializing the variables like I said. It could be that if you press 'Q' right away that certain values are taken from what used to be at that memory location.

    Hope some one more experienced can look at this if those two do not help because I would not knw what could be wrong otherwise.....

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    Dont really get what ur saying with the frist one mate... Yeah I tried initializing the variable as a specific number before the loop... Didn't work... Seems like a simple script but its being very difficult.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    A char can hold a character but also the ASCII value of the character.

    ASCII Code - The extended ASCII table

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    = and == are not the same thing ! Your If conditions are broken.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    So you want me to say: if (act='00101011') for + because the table says
    43 053 2B 00101011 + &#43; Plus

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    a simple
    Code:
    if(act=='+')
    would be enough!
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Registered User
    Join Date
    Dec 2010
    Posts
    21

    Tried it

    I tried using the 0's and ones as well as the & # 4 5 ;(there are spaces so the computer doesn't post it as a +)

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    21
    Stupid that I missed that.

    Look at what 'manasij7479' said. You are not comparing in your if statements you are assigning the value.

    PS. When using the ASCII table to initialize a char you use the decimal number of the ASCII character.

  14. #14
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    Ugh I needed to do if (x==y) instead of if (x=y)... Thats what was wrong... It all works now! Well besides the quiting part, but theres a button for that so...

  15. #15
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    quitting should work fine ..Are you putting in 'Q' or 'q' ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  2. What the heck is wrong with my database?
    By Blizzarddog in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2002, 09:51 AM
  3. What the heck is wrong with this code?
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2002, 02:58 PM
  4. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM
  5. What did i do wrong? Code inside
    By The Rookie in forum C Programming
    Replies: 2
    Last Post: 05-18-2002, 08:14 AM

Tags for this Thread


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