• Welcome to Valhalla Legends Archive.
 

Errors I don't understand.

Started by AntiVirus, October 18, 2004, 07:43 PM

Previous topic - Next topic

AntiVirus


// 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;
}


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!
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

MyndFyre

#1
Quote from: Brandon on October 18, 2004, 07:43 PM

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!

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>
using namespace std;

int main()

{
char Repeat;
float Num_Recruits;
float 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 since the last time you calculated the number of recruits you get per day?" << endl;
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: */
if (Num_Days == 0)
{
  cout << "You can't have zero days!" << endl;
}
else
{


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');
} // end if (Num_Days == 0)
cout << endl;
return 0;
}

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

K


#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;
}

AntiVirus

Quote#include <iostream>
using namespace std;

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

QuoteAnswer = static_cast<float>(Num_Recruits) / Num_Days;
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.
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

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?

AntiVirus

#5
QuoteIt 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?
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;
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

K

it's short for "standard", since you're pulling stuff from the c++ standard template library.

AntiVirus

"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

AntiVirus

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:
QuoteC:\DOCUME~1\Dell\Desktop\CLANRE~1.exe
The NTVDM CPU has encountered an illegal instruction.
CS:0557 IP: ## ## ## ## ## Choose 'Close' to terminate the application

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;
}

Thanks!
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

MyndFyre

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]
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

AntiVirus

#10
Quote[edit]Are you able to generate any input?[/edit]
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:


Does that look right?
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

MyndFyre

No you nincompoop, you're get a compile error!

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


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


[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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

AntiVirus

QuoteNo you nincompoop, you're get a compile error!

Look, there's a period at the beginning of the line just before:
Omfg, I feel so danm stupid.  Thanks a lot!
"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus