Valhalla Legends Forums Archive | C/C++ Programming | Errors I don't understand.

AuthorMessageTime
Brandon
[code]
// Recruitment Calculator
// Created by AntiVirus@uswest

#include <iostream.h>

int main()

{
char Repeat;
int Num_Recruits;
int Num_Days;
float Answer;

do{
cout<<"This was created by AntiVirus@uswest" endl;
cout <<"How many recruits did you, as a clan, get?"
cin>> Num_Recruits;
cout <<"How many days has it been sense the last time you calculated the number of recruits you get per day?"
cin >> Num_Days;

Answer = Num_Recruits / Num_Days;

cout <<"You have gotten " << Answer << " recruits a day." endl;
cout << "Do you want to calculate your results again? (Y or N)"endl;
cin >> Repeat;

} while (( Repeat = "Y" || Repeat = "y"));
cout << endl;
return 0;
}
[/code]

I don't understand the errors in this program when I attempt to compile, would someone be nice enough to compile it and show me the fixed version with comments on what I was doing wrong.  Thanks a lot!
October 19, 2004, 12:43 AM
Myndfyr
[quote author=Brandon link=topic=9225.msg85111#msg85111 date=1098146597]

I don't understand the errors in this program when I attempt to compile, would someone be nice enough to compile it and show me the fixed version with comments on what I was doing wrong.  Thanks a lot!
[/quote]

I'll use [ quote ] tags because I can use formatting.  Bold text means I added something.  Underlined text means I changed or removed something, and you should compare it to what it used to be.

[quote]
// Recruitment Calculator
// Created by AntiVirus@uswest
// Updated extensively, including compilation support (OOOOH!!!) and spelling, by MyndFyre

#include <iostream[u]>[/u]
using namespace std;

int main()

{
char Repeat;
[u]float[/u] Num_Recruits;
[u]float[/u] Num_Days;
float Answer;

do{
cout<<"This was created by AntiVirus@uswest" [u]<<[/u] endl;
cout <<"How many recruits did you, as a clan, get?"[u];[/u]
cin>> Num_Recruits;
cout <<"How many days has it been [u]since[/u] the last time you calculated the number of recruits you get per day?" [u]<< endl;[/u]
cin >> Num_Days;
/* K had a good point here, you should check to make sure that Num_Days is not zero.  However, I would rather do: */
[u]if (Num_Days == 0)
{
  cout << "You can't have zero days!" << endl;
}
else
{ [/u]

Answer = Num_Recruits / Num_Days;

cout <<"You have gotten " << Answer << " recruits a day." [u]<<[/u] endl;
cout << "Do you want to calculate your results again? (Y or N)"[u] << [/u]endl;
cin >> Repeat;

} while [u]([/u] Repeat =[u]=[/u] [u]'Y'[/u] || Repeat =[u]=[/u] [u]'y')[/u];
[u]} // end if (Num_Days == 0)[/u]
cout << endl;
return 0;
}
[/quote]

Now, I have NOT compiled this code.  However, I believe I have gotten most of your syntactical errors out; you forgot several << operators before endl identifiers, as well as several ; before new instructions.  Testing for equality requires two =, for the operator ==, as opposed to the assignment single = operator.  I also changed your int declarations to float because when you perform integer division, you will ALWAYS get an integer back unless you cast before the assignment (which is no longer performing integer division), resulting in a runtime error that, if you couldn't figure out these compilation errors, you would have never caught.

I also fixed the spelling of the word "since."  I have no pity for your inability to distinguish between "sense" (like the 5 senses, a noun) and "since" (which is an abstract, variable measure of time, a preposition), because they are, after all, completely different parts of speech.

Also, when testing for equality, you want to make sure that you're comparing like types.  You don't use "y" to declare a char type, you use single quotes ('y').  Since you declared it as a char and not a char pointer, no matter what the person put in, you would have always quit there.

EDIT: bold apparently does not come through in quotes.  I changed it to italics.
EDIT 2: Apparently neither does italics.  Underline for everything.
October 19, 2004, 1:18 AM
K
[code]
#include <iostream>
using namespace std;

int main()
{
char Repeat;
int Num_Recruits;
int Num_Days;
float Answer;

do
{
cout << "This was created by AntiVirus@uswest" /* missing << here */ << endl;
cout <<"How many recruits did you, as a clan, get?"; // missing semicolon here
cin >> Num_Recruits;
cout <<"How many days has it been sense the last time you calculated the number of recruits you get per day?"; // again, missing semicolon
cin >> Num_Days;

// dividing by zero is bad
if (Num_Days == 0)
continue;

// warning: int/int = int.  cast one to float first
Answer = static_cast<float>(Num_Recruits) / Num_Days;

cout <<"You have gotten " << Answer << " recruits a day." /* missing << here */ << endl;
cout << "Do you want to calculate your results again? (Y or N)"  /* missing << here */ << endl;
cin >> Repeat;

// use single quotes ' ' for characters. " " are for strings.
// also, = is assignment.  this loop will never exit.
// you meant to use ==, the equality operator.
} while (( Repeat == 'Y' || Repeat == 'y'));

cout << endl;

return 0;
}
[/code]
October 19, 2004, 1:19 AM
Brandon
[quote]#include <iostream>
using namespace std;
[/quote]

Why isn't it .h?
And what does "using namespace std;" mean?

[quote]Answer = static_cast<float>(Num_Recruits) / Num_Days;[/quote]
And what is static_cast ?  I understand that it makes Num_Recruits a float but what is static_cast?  When should I use it?

Oh, and thanks a lot you two for your help.
October 19, 2004, 1:51 AM
Mephisto
It isn't .h because most standard libraries that conform to the updated ANSI-C++ standard no longer use the .h extension.  The statement using namespace std; brings all the defined variables, objects, etc. into the scope you declare that statement (in this case, global scope).  Keep in mind that all of the standard iostream objects are declared in this namespace, so it's necessary to declare that statement so you can use them (in the scope you're using them, global usually being best).  You could've also used statements such as using std::cout; but using namespace std; just brings everything in (including a lot of uneeded things).

The static_cast (among reinterpret, const, and dynamic) is one of four new cast operators added to C++ as an extension to C.  Before the casting operators were around, there was one universal casting operator (or rather, typecasting) which was () (there are other operators that use the same symbol so don't get confused) which would be used like (int)x; which would have the compiler treat the variable x as type int even if it wasn't.  The casting operators are more specific in what they actually do as far as typecasting is concerned.  The static cast operator is probably the closest to the universal typecasting operator found in C, with the reinterpret cast behind it.  It operates on standard non-pointer types (int, float, double, etc.) while the reinterpret cast operators on pointers of standard types.  The syntax for all casting operators is: TypeOfCast_cast<type>(expression); -- the expression is the variable, and the type is the type you want to convert it into essentially (though literally, you're not converting it, you're just having the compiler recognize it as that type for that statement).

You should use the static_cast operator whenever you want to typecast on a standard non-pointer type such as int to float.

Does this help?
October 19, 2004, 2:50 AM
Brandon
[quote]It isn't .h because most standard libraries that conform to the updated ANSI-C++ standard no longer use the .h extension.  The statement using namespace std; brings all the defined variables, objects, etc. into the scope you declare that statement (in this case, global scope).  Keep in mind that all of the standard iostream objects are declared in this namespace, so it's necessary to declare that statement so you can use them (in the scope you're using them, global usually being best).  You could've also used statements such as using std::cout; but using namespace std; just brings everything in (including a lot of uneeded things).

The static_cast (among reinterpret, const, and dynamic) is one of four new cast operators added to C++ as an extension to C.  Before the casting operators were around, there was one universal casting operator (or rather, typecasting) which was () (there are other operators that use the same symbol so don't get confused) which would be used like (int)x; which would have the compiler treat the variable x as type int even if it wasn't.  The casting operators are more specific in what they actually do as far as typecasting is concerned.  The static cast operator is probably the closest to the universal typecasting operator found in C, with the reinterpret cast behind it.  It operates on standard non-pointer types (int, float, double, etc.) while the reinterpret cast operators on pointers of standard types.  The syntax for all casting operators is: TypeOfCast_cast<type>(expression); -- the expression is the variable, and the type is the type you want to convert it into essentially (though literally, you're not converting it, you're just having the compiler recognize it as that type for that statement).

You should use the static_cast operator whenever you want to typecast on a standard non-pointer type such as int to float.

Does this help? [/quote]
Ya, a lot.  Thanks so much!  Ooh, and one more question.  What does "std" stand for?  And I don't mean the sexually transmitted diseases, I mean the using namespace std;
October 19, 2004, 8:32 PM
K
it's short for "standard", since you're pulling stuff from the c++ standard template library.
October 19, 2004, 9:22 PM
Brandon
Alright, thanks K.
October 19, 2004, 9:26 PM
Brandon
Alright, I have yet another error I don't understand.  Everything works all fine and dandy until I attempt to save it as a .exe and assign it to my desktop.  I double click the program and I get this message:
[quote]C:\DOCUME~1\Dell\Desktop\CLANRE~1.exe
The NTVDM CPU has encountered an illegal instruction.
CS:0557 IP: ## ## ## ## ## Choose 'Close' to terminate the application[/quote]

DarkVirus told me that I need to debug and he said that he may be able to help me if I gave him the code, so I did and he never responded back.  I have no idea what to do while I am debuging because I never have before, could someone please help me?  And my code is this:
[quote]
#include <iostream>
using namespace std;

int main()
{
char Repeat;
int Num_Recruits;
int Num_Days;
float Answer;

do
{
cout << "This was created by AntiVirus@uswest, and a lot of help from K@vLFroums" << endl;
cout <<"How many recruits did you, as a clan, get?";
cin >> Num_Recruits;
cout <<"How many days has it been sense the last time you calculated the number of recruits you get per day?";
cin >> Num_Days;


if (Num_Days == 0)
continue;


Answer = static_cast<float>(Num_Recruits) / Num_Days;

cout <<"You have gotten " << Answer << " recruits a day."<< endl;
cout << "Do you want to calculate your results again? (Y or N)" << endl;
cin >> Repeat;


} while (( Repeat == 'Y' || Repeat == 'y'));

cout << endl;

return 0;
}
[/quote]

Thanks!
October 19, 2004, 11:09 PM
Myndfyr
If you're using Visual Studio, select the first line of your main() function and press CTRL+B to set a breakpoint.  Then press F5 to compile and start execution, which will break at the line where you set the breakpoint.

Execute line-by-line with the toolbar controls in VS until you get your runtime error.  Then let us know which line is the offensive one.

[edit]Are you able to generate any input?[/edit]
October 19, 2004, 11:47 PM
Brandon
[quote][edit]Are you able to generate any input?[/edit] [/quote]
Ya, when I execute the program in my compiler, but not from my desktop.

Alright, I did that break thing then hit run and then I got this:
[img]http://img99.exs.cx/img99/7451/CRSS2.jpg[/img][img]http://img99.exs.cx/img99/5103/crss.jpg[/img]

Does that look right?
October 19, 2004, 11:51 PM
Myndfyr
No you nincompoop, you're get a compile error!

Look, there's a period at the beginning of the line just before:

[code]
} while (( Repeat == 'Y' || Repeat == 'y' ));
[/code]

[edit]Aside from that, yes, you set the breakpoint correctly.  My guess will be, though, that you won't get the error anymore; the linker was generating bad output causing your RTE, I'll bet.
October 20, 2004, 2:42 AM
Brandon
[quote]No you nincompoop, you're get a compile error!

Look, there's a period at the beginning of the line just before:[/quote]
Omfg, I feel so danm stupid.  Thanks a lot!
October 20, 2004, 9:41 PM

Search