GP32 What's Wrong With This Code (compiling Errors)?


Azure

Trust the recursion...
Joined
May 21, 2003
Messages
3,805
Location
California, USA
I'm trying to compile a simple program that adds numbers, but I get 2 error messages when trying to compile:
Error_box.jpg


Here's my code:
Code:
// Number Adder Upper - Add up numbers constantly to an original number
// until the value of the origin reaches a value greater than 255
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
int origin;
origin = 0;
int number1;
while (number1 < 255)
{
cout << "Enter a number to add up";
cin >> number1;

origin = origin + number1;
cout << "Your current number is" << origin
}
}

What's wrong?
 

Attachments

  • Error_box.jpg
    Error_box.jpg
    10.3 KB · Views: 188
  • Error_box.jpg
    Error_box.jpg
    10.3 KB · Views: 184
  • Error_box.jpg
    Error_box.jpg
    10.3 KB · Views: 190
Hmm...when I ran it, it didn't display any of my code (except the part that waits for the user to terminate the program by pressing a key). Not even my "cout" was shown. Any idea on what I did wrong?

EDIT: Doesn't the user give "number1" a value in the "cin" line?
 
Hmm...when I ran it, it didn't display any of my code (except the part that waits for the user to terminate the program by pressing a key). Not even my "cout" was shown. Any idea on what I did wrong?

EDIT: Doesn't the user give "number1" a value in the "cin" line?

I think you're using an IDE, right? Maybe Visual Studio? Some IDEs require that you press a key before the program officially "ends". That is what is happening, everything in the while loop is not being executed. Now why is that happening?

Unlike Java, most C / C++ compilers do NOT automatically initialize a basic variable (like int).

OK, you declared "int number1". That means the variable "number1" points to a 4-byte block of memory. This block of memory can have ANYTHING inside of it. It's not like they "clean" memory out when they're done with it, unless you specifically do it yourself. So let's say this block of memory used to contain a pointer, which is usually a very large number, let's say: 6452123. Now let's cruise through your code.

Code:
int number 1;
< allocate 4-byte block to variable >
< number1 = 6452123, what was left in memory >
Code:
while (number1 < 255)

< this evaluates to 6452123 < 255, and which is obviously false, so the while loop does not continue >

So as you see, everything inside the while loop is getting skipped. Just do int number1 = 0; and your program should work fine.
 
Code:
// Number Adder Upper - Add up numbers constantly to an original number
// until the value of the origin reaches a value greater than 255
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{

int origin;
origin = 0;
int number1;
number1 = 0;

{

cout << "Hello. This program will add up numbers that you enter to a starting\n original number. This program will stop adding numbers once the sum obtains a value greater than 255.\n";
cout << "\nEnter a number: \n";
cin >> number1;

origin = origin + number1;
cout << "Your current number is " << origin << ".";
}

int current1;
current1 = 0;
cout <<"\nEnter a number to add to your current number: ";
cin >> current1;
origin = origin + current1;
cout << "Your current number is " << origin;

while (origin < 255);
{
int another1;
another1 = 0;
cout <<"\n Enter another number to add to your current number: ";
cin >> another1;

origin = origin + another1;
cout << "\n Your current number is " << origin << ".";
}

system("PAUSE");
return 0;
}
It never gets pass the second "Your current number is." So, it stops right on the while (I'm guessing the conditional isn't true).

BTW, I'm using Dev-C++.
 
Code:
{

cout << "Hello. This program will add up numbers that you enter to a starting\n original number. This program will stop adding numbers once the sum obtains a value greater than 255.\n";
cout << "\nEnter a number: \n";
cin >> number1;

origin = origin + number1;
cout << "Your current number is " << origin << ".";
}

You can't section out your code like this in C++. You need to have a while, for, function prototype, etc. before the { (open bracket). It's probably treating the closing bracket as the closing for the function (int main) itself. In short, remove the two brackets in this section of code.

Edit: On second thought, this post really belongs in New Developers Help...even though I'm the one that told Azure to post in Developer's Corner in the first place :D

Edit2: I'm going to sleep now...please be gentle to Azure or I go into super Nazi mode ;)
 
Azure posted on Jul 21 2004 at 03:38 AM said:
Code:
while (origin < 255);
{
int another1;
another1 = 0;
cout <<"\n Enter another number to add to your current number: ";
cin >> another1;

origin = origin + another1;
cout << "\n Your current number is " << origin << ".";
}

system("PAUSE");
return 0;
}
It never gets pass the second "Your current number is." So, it stops right on the while (I'm guessing the conditional isn't true).

BTW, I'm using Dev-C++.
When you say it never gets past the second "Your current number is." do you mean that your program just finishes? If so, it means that your origin variable is greater than 254. try running the prog with extremely low numbers.
 
Last edited by a moderator:
Whoa! :huh:

Are you practicing typing??

This is one loop opened up!!
This would do the job just as well ...
Code:
int origin= 0;
int number1= 0;

cout << "Hello. This program will add up numbers that you enter to a starting\n original number. This program will stop adding numbers once the sum obtains a value greater than 255.\n";

while(origin<255)
{
cout << "\nEnter a number: \n";
cin >> number1;

origin = origin + number1;
cout << "Your current number is " << origin << ".";
}
The problem you are having is that your final while shouldn't have a semi-colon after it.
Easily missed .. even by experienced coders who auto-semi-colon lots of lines :lol:


btw. bracketing out sections of code with { ... } is valid or else there would be strange errors trying to
Code:
if(foo) bar1(); else { bar2(); }

Hope this helps and not confuses ;)
If gNMX doesn't like it -- I WAS JOKING!! :wacko: :D

ps. I hope there are none of my typos in here ;)
 
I was running it with low values everytime. It doesn't matter anyways as I got it to work. I just had to clean some things here and there and it worked perfectle.

BTW, I'm thinking about making this thread my official thread for posting my code and asking what's wrong with it (I won't do it too often, as in everytime I encounter an error, but mostly only after I encounter an error that I spend at least 30 minutes trying to solve, and after those 30 minutes, I'll most likely just go to #GP32Dev first and ask there). Is that okay? Or should this remain like an official thread for everybody where anybody can post their code and ask why it won't compile properly?
 
ahhh... just realised what your problem was...

Code:
while (origin < 255);

you had a semi-colon at the end of this line - which will basically break the while statement. you needed it like this:

Code:
while (origin < 255)

It is important that for any conditional statement you do not have a semi-colon at the end of the line.

EDIT - I just realised that someone has already told you this... never mind
 
Hope this helps and not confuses
If gNMX doesn't like it -- I WAS JOKING!!

Bracketing out sections of code without an "else", "if", "while", "do", "switch", or function prototype can get some compilers confused. I've never used Dev-C++, so I'm not sure if it sucks like that or not :D Usually those compiler errors have been fixed in modern compilers.

But you noticed something I didn't, my exhaustion was no excuse. My bad ;)
 
Charge posted on Jul 21 2004 at 06:43 AM said:
ahhh... just realised what your problem was...

Code:
while (origin < 255);

you had a semi-colon at the end of this line - which will basically break the while statement. you needed it like this:

Code:
while (origin < 255)

It is important that for any conditional statement you do not have a semi-colon at the end of the line.
Except when you use "do," right?
Code:
do{
cout << "Enter haste percentage: ";
cin >> hastep;

newweap = hastep + 100;
newweap = 100 / newweap;

cout << "Enter weapon delay: ";
cin >> weapd;

newweap = newweap * weapd;

cout << "Your new weapon delay is " << newweap << "\n\n";
}
while (weapd > 9);
I leaned about the do command on IRC (Which makes me mad because I don't know why the book didn't tell me about it. I remember it only said "do" in a code example, but it was never mentioned in any of the text, so I just ignored it), and found out that you do the opposite of while (You put the while with the conditional at the end and you use a semi-colon).
 
Last edited by a moderator:
Azure posted on Jul 21 2004 at 09:10 PM said:
I leaned about the do command on IRC (Which makes me mad because I don't know why the book didn't tell me about it. I remember it only said "do" in a code example, but it was never mentioned in any of the text, so I just ignored it), and found out that you do the opposite of while (You put the while with the conditional at the end and you use a semi-colon).
You are correct about putting a semi-colon at the end of the while command if it is part of a do loop. I had completely forgotten the command myself as I never use it. If your C++ book is a beginners book then there will be a LOT of things that are not covered so I wouldn't worry about it. The beginners books are introductions to programming methodology with a some of the syntax. When you fully understand your current book you could get a 1000+ page book - which will cover all the commands + syntax.

The do command itself just makes sure that the code in the condition is executed at least once, it is repeated if you meet the while(condition) at the end of the do{} - wheras with just a while(condition) you may never execute the code (if you don't meet the condition).
 
Last edited by a moderator:
Charge posted on Jul 21 2004 at 09:50 PM said:
You are correct about putting a semi-colon at the end of the while command if it is part of a do loop. I had completely forgotten the command myself as I never use it.

do loops are handy for producing fast compiled code, as in assembler they only require one branch and one comparison opcode per iteration.

(while loops need two branch opcodes)
 
Last edited by a moderator:
Ok, now what's wrong with this code snippet (Trying to get functions to work):
Code:
double dvalue = 0;
double sqaure = 0;
cout << "Now you enter in a number that you want to square:";
cin >> dvalue;

square(dvalue) = square;

double square (double doublevar)
{
return doublevar * doublevar;
}
I'm having such a hard time with functions (Though, in the book, I'm way past them.) *sigh*
 
Azure posted on Jul 29 2004 at 05:23 AM said:
Ok at now what's wrong with this code snippet (Trying to get functions to work):
Code:
double dvalue = 0;
double sqaure = 0;
cout << "Now you enter in a number that you want to square:";
cin >> dvalue;

square(dvalue) = square;

double square (double doublevar)
{
return doublevar * doublevar;
}
I'm having such a hard time with functions (Though, in the book, I'm way past them.) *sigh*
When you are writing a prog you will start it by writing in a function named Main. It will look something like this:

Code:
void main()
{
   int nMynum = 0;

   nMynum += 2;
}

You have to remember that main() is a function itself, its just that a computer knows that it is the one to start running, rather than starting with another function. Since it is a function it must start with an open bracket { and finish with a close bracket }.

All functions that you write have to be seperated from each other. For example:

Code:
// Declare function prototypes
int MySquareFunction(int nInput);


void main()
{
   double nMynum = 0.0;
   double nMynum2 = 0.0;

   nMynum += 2.95;
   nMynum2 = MySquareFunction(nMyNum);
}

double MySquareFunction(int nInput)
{
   return nInput * nInput;
}

Note how the close bracket of main() } is before the next function. In your code there is no close bracket before the square function.

Another very important thing is that learning to code takes quite some time, and most people (wrongly) give up because it is too hard. The thing is that it just takes time, probably a year before you will understand programming concepts and a language well enough to write your own good programs. As long as you keep trying it will come to you. :)
 
Last edited by a moderator:
Thanks for the reply, but the nice people on #GP32Dev (Especially CrazyDesi) answered my questions earlier. But thanks anyways.
 
Having some trouble compiling this code:
Code:
//
//
//
//
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;

class account
{
    public:
        double balance = 0;
        string name;
        static numberofaccounts;
        account (string accountname)
        {
            cout << "Constructing account...";
            cout << "Account constructed.";
            name = accountname;
            numberofaccounts = ++numberofaccounts;
        }
        ~account ()
        {
            cout << "Destructing accounts...";
        }
            
        void currentbalance ()
        {
            cout << "Your current balance is " << balance << ".";
        }    
        
        void deposit ()
        {
            int depositcorrect = 1;
            int depositquit = 1; 
            double depositamount = 0;
            cout << "You have selected the deposit function.";
                
            for (;;)
            {
                cout << "Please enter in the amount you wish to deposit: ";
                cin >> despositamount;
                cout << "You entered " << despositamount;
                cout << "If this is correct, enter 1, if not enter 2.\n";
                cout << "Enter in any number other than 1 or 2 to exit the deposit function.\n";
                cin >> depositcorrect;
                
                if (depositcorrect < 1 || depositcorrect > 2)
                {
                    cout << "Now exiting the deposit funcion...";
                    break;
                } 
                
                if (depositcorrect == 1)
                {     
                    balance += depositamount;
                    cout << "You have just deposited" << depositamount << " dollars into your account.";
                    cout << "You're current balance now is " << balance << ".";
                    cout << "Enter 1 to make another deposit, otherwise, enter any other number to quit this function";
                    cin >> depositquit;
                    if (depositquit != 1)
                    {
                        break;
                    }
                }
                
                if (depositcorrect == 2)
                {
                    cout << "You have chosen 2, in that the deposit amount enter is incorrect.";
                }
            }
        }
        
        double withdraw ()
        {
            double withdrawlamount = 0;
            int withdrawlcorrect = 1;
            int withdrawlquit = 1;
            cout << "You have selected the withdrawl function.";
                
            for (;;)
            {
                cout << "Please enter in the amount you with to withdraw: ";
                cin >> withdrawlamount;
                cout << "You entered " << withdrawlamount;
                cout << "If this is correct, enter 1, if not, enter 2.\n";
                cout << "Enter in any number other than 1 or 2 to exit the withdrawl function.\n";
                cin >> withdrawlcorrect;
                
                if (withdrawlcorrect < 1 || withdrawlcorrect > 2)
                {
                    cout << "Now exiting the withdrawl function...";
                    break;
                }
                
                if (withdrawlcorrect == 2)
                {
                    cout << "You have chosen 2, in that the deposit amount enter is incorrect.";
                }
                
                if (withdrawlcorrect == 1)
                {
                    balance -= withdrawlamount;
                    cout << "You have just withdrawn " << withdrawlamount << " dollars from your account.";
                    cout << "Your current balance now is " << balance << ".";
                    cout << "Enter 1 to make another withdrawl, otherwise, enter any other number to quit this function";
                    return withdrawlamount;
                }
            }
        }
};
                
                


int main (int nNumberofArgs, char* pszArgs[])
{
    double deposit;
    double withdrawl;
    double cash;
    
    string accountname;
    
    cout << "Enter in the name of the account you would like to create: ";
    cin >> accountname;
    account account1(accountname);
    
    cout << "Enter 'd' to deposit money into your account.";
    cout << "Enter 'w' to withdraw money from your account.";
    cout << "Enter 'b' to view your accont's balance.";
    
    switch (choice)
    {
        case 'd':
        account1.deposit;
        
        case 'w':
        cash = account1.withdraw;
        
        case 'b':
        account1.currentbalance;
    }
    
    system ("Pause");
    return 0;
}
Here are the compiling errors:
compilingerrors.jpg
 

Attachments

  • compilingerrors.jpg
    compilingerrors.jpg
    49.5 KB · Views: 192
  • compilingerrors.jpg
    compilingerrors.jpg
    49.5 KB · Views: 182
  • compilingerrors.jpg
    compilingerrors.jpg
    49.5 KB · Views: 179
You can't say double balance =0;

You need to just have double balance;
Then do balance=0; in your constructor.

You never declared the variable choice.(nor did you put anything into it.)

You spelled deposit wrong on line 45

Also....you probably want to have break statements in your switch.

case 'd':
<do whatever>
break;

Otherwise it will execute that case ans every case under it.

I think it should also be account.deposit();

Try those and then let's see what you have left.
 
I fixed them, now it's this:
compilingerrors3.jpg
 

Attachments

  • compilingerrors3.jpg
    compilingerrors3.jpg
    13.8 KB · Views: 192
  • compilingerrors3.jpg
    compilingerrors3.jpg
    13.8 KB · Views: 178
  • compilingerrors3.jpg
    compilingerrors3.jpg
    13.8 KB · Views: 173
Back
Top