Author Topic: My first C++ Project  (Read 6597 times)

0 Members and 1 Guest are viewing this topic.

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
My first C++ Project
« 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
Code: [Select]
#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
Code: [Select]
"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
Code: [Select]
#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
« Last Edit: January 23, 2011, 12:22:22 am by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: My first C++ Project
« Reply #1 on: January 22, 2011, 12:16:41 pm »
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.
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My first C++ Project
« Reply #2 on: January 22, 2011, 12:18:48 pm »
OK I'll try that
Did you see any problems with the code?

EDIT: You were right The path doesn't exist
« Last Edit: January 22, 2011, 12:31:28 pm by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: My first C++ Project
« Reply #3 on: January 22, 2011, 12:32:04 pm »
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.
« Last Edit: January 22, 2011, 12:33:14 pm by Binder News »
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline shrear

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 193
  • Rating: +17/-0
    • View Profile
Re: My first C++ Project
« Reply #4 on: January 22, 2011, 12:38:49 pm »
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.
« Last Edit: January 22, 2011, 12:40:22 pm by shrear »

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My first C++ Project
« Reply #5 on: January 22, 2011, 12:58:58 pm »
Here is my new code
Code: [Select]
#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?
« Last Edit: January 22, 2011, 01:42:13 pm by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: My first C++ Project
« Reply #6 on: January 22, 2011, 01:49:08 pm »
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.
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My first C++ Project
« Reply #7 on: January 22, 2011, 02:17:56 pm »
more code should work now
Code: [Select]
#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
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: My first C++ Project
« Reply #8 on: January 22, 2011, 02:19:32 pm »
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++.

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My first C++ Project
« Reply #9 on: January 22, 2011, 02:35:15 pm »
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:
Code: [Select]
#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;
}
« Last Edit: January 22, 2011, 02:57:09 pm by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: My first C++ Project
« Reply #10 on: January 22, 2011, 02:56:36 pm »
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.
Code: [Select]
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.
« Last Edit: January 22, 2011, 02:58:46 pm by Binder News »
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My first C++ Project
« Reply #11 on: January 22, 2011, 03:12:11 pm »
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
Code: [Select]
#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");
}
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: My first C++ Project
« Reply #12 on: January 22, 2011, 03:42:53 pm »
You need to change
Code: [Select]
cout << "Your answer is", answer;
to
Code: [Select]
cout << "Your answer is " << answer << endl; //endl = \n
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: My first C++ Project
« Reply #13 on: January 22, 2011, 04:22:11 pm »
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
Code: [Select]
#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");
}
« Last Edit: January 22, 2011, 05:20:26 pm by ruler501 »
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: My first C++ Project
« Reply #14 on: January 22, 2011, 06:04:01 pm »
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.
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results: