Omnimaga
General Discussion => Technology and Development => Computer Projects and Ideas => Topic started by: ruler501 on January 22, 2011, 12:07:53 pm
-
I am working on my first real C++ Project a base to base converter that will work with all real numbers 36 and other.
This is the beginning of my code. currently it should only supports numbers from 1 to 10
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int product, power;
float num, rem;
double answer, base, conv=1, ten, place;
power = 0;
ten = 10.0;
answer = 0;
cout << "Enter number to be converted ";
cin >> num;
cout << "\n";
cout << "What base do you want to put it in ";
cin >> base;
cout << "\n";
rem = num;
while(base>10)
{
cout << "please put in a smaller base ";
cin >> base;
cout << "\n";
}
while(conv<=num)
{
power++;
conv=conv*base;
}
while(rem!=0)
{
power--;
conv=pow(base, power);
place = pow(ten, power);
product = floor(rem/conv);
while(product >= base)
{
product-=1;
}
answer += product*place;
rem -= product*conv;
if(power<=-20)
{
break;
}
}
cout << "Your answer is " << answer <<"\n";
cout << "\n";
system("PAUSE");
}
I'm using codeblocks with the gnu gcc compiler it returns this error when I try to compile please help
"B2B - Release" uses an invalid compiler. Probably the toolchain path within the compiler options is not setup correctly?! Skipping...
Nothing to be done.
EDIT: I am now using Microsoft Visual C++ Express 2010 and have a succesful beta build which I have attached here. Updated my code above
EDIT2:Here is the code for my Quadratic Solver
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float bs, cs, s, ra, rb, a, b, c, h, k;
cout << "What is a? ";
cin >> a;
cout << "What is b? ";
cin >> b;
cout << "\n";
cout << "What is c? ";
cin >> c;
cout << "\n";
bs = b/a;
cs = -1*c/a+pow(bs, 2)/4;
s = sqrt(cs);
ra = bs/2+s;
rb = bs/2-s;
h=bs/2;
k=a*pow(bs, 2)/4+c;
cout << "The First Root is" << ra << "\n";
cout << "the Second Root is" << rb << "\n";
cout << "Vertex is (" << h << "," << k << ")" << "\n";
system("Pause");
}
I attached the .exe for this also
-
Got to Settings->Compiler->Toolchain Executables. Make sure that the path selected is where the executables actually are (go into Explorer, find the folder, make sure the files exist)
The other thing is, if, when you made the project, you accidentally selected a different compiler for the "Release" option, then you should probably just delete the old project files, make a new project with the same name, CHECK YOUR "Release" configuration to make sure yo are using the right compiler, then import the existing source files.
-
OK I'll try that
Did you see any problems with the code?
EDIT: You were right The path doesn't exist
-
No, I didn't really check for effectiveness. I just don't think it'll crash. Except, you might want to check on that ^ operator, I think it's actually the XOR operator in C++. If you are looking for the remainder operator (like the ^ operator in Axe), it is %.
And I don't think you need to include the stdio.h/cstdio lib, either.
EDIT: I suggest re-installing Code::Blocks.
-
There are some small errors:
power is undeclared (if it is a variable, don't really get your code there)
you can't use, as said by Binder News, the ^ operator like you do.
there is a missing ;
produt isn't product ;)
and there is one place where you mix int and float, this may or may not cause problems.
I wish you Good luck in writing this program.
-
Here is my new code
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int product, power;
float num, base, rem, conv=1, place;
double answer;
cout << "Enter number to be converted";
cin >> num;
cout << "What base do you want to put it in";
cin >> base;
while(base>10)
{
cout << "please put in a smaller number";
cin >> base;
}
while(conv<num)
{
power++;
conv=conv*base;
}
while(rem!=0)
{
power-=1;
conv=base^power;
place = 10^power;
product = rem/conv;
while(product >= base)
{
product-=1;
}
answer += product*place;
rem -= product*conv;
if(rem<0)
{
answer -= product*place;
rem += product*conv;
break;
}
}
}
How to you raise a number to an exponent in C++?
EDIT: the includes are what my book said to do
EDIT 2 what is a good compiler to use?
-
The GNU compiler (which is what Code::Blocks comes with) is perfectly fine.
Here is the site I use as a reference for the standard C and C++ libraries: http://www.cplusplus.com/reference/clibrary/cmath/
NOTE: That specifically links to math.h
You don't need the cstdio include. Trust me. The iostream will work fine.
-
more code should work now
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int product, power;
float num, rem, conv=1, place;
double answer, base, ten;
cout << "Enter number to be converted";
cin >> num;
cout << "\n";
cout << "What base do you want to put it in";
cin >> base;
cout << "\n";
while(base>10)
{
cout << "please put in a smaller number";
cin >> base;
cout << "\n";
}
while(conv<num)
{
power++;
conv=conv*base;
}
while(rem!=0)
{
power--;
conv=pow(base, power);
place = pow(ten, power);
product = rem/conv;
while(product >= base)
{
product-=1;
}
answer += product*place;
rem -= product*conv;
if(rem<0)
{
answer -= product*place;
rem += product*conv;
break;
}
}
}
compiler still not working can't find it gnu gcc doesn't work
-
Nice to see you're using While loops instead of For. You can tell I love While.
I'd recommend Visual Studio as a C++ compiler. I hate Code: Blocks and like a bit DevC++.
-
EDIT: I rewrote some of it and still get some errors and cause this to happen when I run the program
Enter number to be converted 50//the 50 is what I put in
What base do you want to put it in 5//the 5 is what I put in
It then leaves the cursor on the next line and runs forever without showing anything
this is my code:
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int product, power;
float num, rem;
double answer, base, conv=1, ten, place;
cout << "Enter number to be converted ";
cin >> num;
cout << "\n";
cout << "What base do you want to put it in ";
cin >> base;
cout << "\n";
while(base>10)
{
cout << "please put in a smaller base ";
cin >> base;
cout << "\n";
}
while(conv<=num)
{
power++;
conv=conv*base;
}
while(rem!=0)
{
power--;
conv=pow(base, power);
place = pow(ten, power);
product = floor(rem/conv);
while(product >= base)
{
product-=1;
}
answer += product*place;
rem -= product*conv;
if(power<=-20)
{
break;
}
}
cout << "Your answer is", answer;
}
-
First, you must initialize power, rem, ten, and answer. You need to store a value to them. Num is initialized when you store whatever cin is to it.
power = 0;
rem = 0;
ten = 0;
answer = 0;
The reason I dislike using Visual Studio is because it forces you to make a "solution" for EVERY project.
[rant] I JUST WANT TO COMPILE MY FRICKIN 1 FILE PROGRAM!!!! [/rant]
Yeah, anyways, I still suggest Code::Blocks, unless you can't get it to work. I just prefer the UI.
-
I like it better just it doesn't work
[rant]I JUST WANT TO USE A GOOD PROGRAM[/rant]
It doesn't display an answer
here's my newest code
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int product, power;
float num, rem;
double answer, base, conv=1, ten, place;
power = 0;
rem = 0;
ten = 0;
answer = 0;
cout << "Enter number to be converted ";
cin >> num;
cout << "\n";
cout << "What base do you want to put it in ";
cin >> base;
cout << "\n";
while(base>10)
{
cout << "please put in a smaller base ";
cin >> base;
cout << "\n";
}
while(conv<=num)
{
power++;
conv=conv*base;
}
while(rem!=0)
{
power--;
conv=pow(base, power);
place = pow(ten, power);
product = floor(rem/conv);
while(product >= base)
{
product-=1;
}
answer += product*place;
rem -= product*conv;
if(power<=-20)
{
break;
}
}
cout << "Your answer is", answer;
cout << "\n";
system("PAUSE");
}
-
You need to change
cout << "Your answer is", answer;
to
cout << "Your answer is " << answer << endl; //endl = \n
-
It responds 5 is 0 in base 5 with this code
EDIT: I changed rem to start with the value of num it now says five is 1 in base 2 and 50 is 0 in base 2
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int product, power;
float num, rem;
double answer, base, conv=1, ten, place;
power = 0;
ten = 0;
answer = 0;
cout << "Enter number to be converted ";
cin >> num;
cout << "\n";
cout << "What base do you want to put it in ";
cin >> base;
cout << "\n";
rem = num;
while(base>10)
{
cout << "please put in a smaller base ";
cin >> base;
cout << "\n";
}
while(conv<=num)
{
power++;
conv=conv*base;
}
while(rem!=0)
{
power--;
conv=pow(base, power);
place = pow(ten, power);
product = floor(rem/conv);
while(product >= base)
{
product-=1;
}
answer += product*place;
rem -= product*conv;
if(power<=-20)
{
break;
}
}
cout << "Your answer is " << answer <<"\n";
cout << "\n";
system("PAUSE");
}
-
Why don't you start with something a little simpler? Say, a quadratic formula program?
The main problem is that the result can't be held as a number. It needs to be a string.
I attached some code I made in about 10 minutes. It doesn't work, but it's a start.
I really think you should start with a simpler program though, as converting bases is a little tricky. Especially in an new language.
-
It responds 5 is 0 in base 5 with this code
EDIT: I changed rem to start with the value of num it now says five is 1 in base 2 and 50 is 0 in base 2
In your example... conv is initialized as 1 and num is set to 5 at runtime. Hence, power will be 0 when you begin your last while loop (the first thing you do there is power--). DO you really want a negative value for power?
Also... you have this variable called ten, but you initialize it to 0. Does that makes sense? Consider your line of code place = pow (ten, power).
-
thank You for pointing out that I initialized ten to zero I fixed it now and my program is giving me correct results so far
I only had to initialize ten to 10
EDIT: I attached it in all of its simpleness now have to make it support larger numbers
EDIT2: I moved it to my first post
-
thank You for pointing out that I initialized ten to zero I fixed it now and my program is giving me correct results so far
I only had to initialize ten to 10
Awesome!
-
I attached the executable to my post if you want to show me all of the bugs :'(
-
Very nice! I might have a use for this. I guess I was wrong about doing a less complicated first program.
-
i had to ask a few questions but i understood the concept of what i was doing. I'll come back to this later when i learn more to add more number capability to it.
I'll post my multi-method quadratic solver later also I'll just use the same method my calculator does in the program I made for it. Just have to finish it.
EDIT: anyways i like challenges
EDIT2: I posted the code for and the program of my Quadratic Solver on my first post. It solves quadratics by the completing the Square method.