Can anyone find a problem in this VC++ code?

Can anyone find a problem in this VC++ code?

Post by bird7727 on Sun Feb 22, 2009 4:12 am
([msg=18389]see Can anyone find a problem in this VC++ code?[/msg])

I am writing this algebraic calculator for some more experience, but I've hit a road block. The math is working fine so far, it just won't display the answer. With the cout<<"The answer is "<<a1<<"\n"; uncommented, it says that I have not initialized the variable. Any help?

Code: Select all
// AlgebraCalculatorWIN32.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
   int type;
   bool monomial;
   bool binomial;



   cout<<"Hello.\n\n";
   cout<<"If your problem is a monomial, enter 1.\n";
   cout<<"If your problem is a binomial, enter 2.\n";
   cin>>type;

   if (type == 1){
      cout<<"Your problem is a monomial.\n";
      monomial = 1;
      binomial = 0;
   }
   if (type == 2){
      cout<<"Your problem is a binomial.\n";
      monomial = 0;
      binomial = 1;
   }

   /* else function is broken.
   else {
      cout<<"Enter 1 or 2 next time!\n";
      monomial == 0;
      binomial == 0;
   }
   */

   int num1;
   int num2;
   //int num3; Uncomment when TRINOMIAL support is added
   int exp1;
   int exp2;
   //int exp3; Uncomment when TRINOMIAL support is added
   int a1;
   int a2;
   //double answer3; Uncomment when TRINOMIAL support is added

   while (monomial == 1) {
      cout<<"Now, what is the first term's number?\n";
      cin>>num1;
      cout<<"The monomial's number is "<<num1<<"\n";
      cout<<"What is the exponent?\n";
      cin>>exp1;
      cout<<"The exponent is "<<exp1<<"\n\n";
      (num1*(num1*exp1))==a1;

      cout<<"The answer is "<<a1<<"\n\n";
      
   }



   system("PAUSE");
   return 0;
}
bird7727
New User
New User
 
Posts: 48
Joined: Sun Feb 01, 2009 12:43 am
Blog: View Blog (0)


Re: Can anyone find a problem in this VC++ code?

Post by BhaaL on Sun Feb 22, 2009 6:30 am
([msg=18398]see Re: Can anyone find a problem in this VC++ code?[/msg])

You might want to assign your result, rather than comparing it.

Code: Select all
//this compares a1 (with its default value 0) to your result
//(num1*(num1*exp1))==a1;
//this stores the result of it in a1
a1 = (num1*(num1*exp1));
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Can anyone find a problem in this VC++ code?

Post by Ninfa on Sun Feb 22, 2009 6:56 am
([msg=18399]see Re: Can anyone find a problem in this VC++ code?[/msg])

== is used for comparison, use = when you want to attribute a value.

If that expression is meant to calculate the result of num1 to the power of exp1 (num1^exp1), it's not gonna be really effective. Either use the pow(x,y); function on math.h library or do something like this:

Code: Select all
a1 = num1;
for (int i=1;i<exp1;i++) {a1 *= num1;}
Ninfa
New User
New User
 
Posts: 16
Joined: Sun Feb 22, 2009 1:57 am
Location: Behind you.
Blog: View Blog (0)


Re: Can anyone find a problem in this VC++ code?

Post by bird7727 on Sun Feb 22, 2009 3:53 pm
([msg=18420]see Re: Can anyone find a problem in this VC++ code?[/msg])

Thanks for your help. Ninfa's code worked well and I'm going to look into using math.h functions later in development.
bird7727
New User
New User
 
Posts: 48
Joined: Sun Feb 01, 2009 12:43 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests