-
Newb question
I'm really new, really really new, to C++, and all programming for that matter. I just picked it up as a personal goal, and I'm having trouble implementing my previous lessons from the tutorial into my current one.. Most currently with using a loop along with the function lesson code... Let me show you:
Code:
#include <iostream>
using namespace std;
int mult ( int x, int y );
int main()
{
for ( x == 0; x < 10; x++ ) {
int x;
int y;
cout<<"Input two numbers for multiplication: ";
cin>> x >> y;
cin.ignore();
cout<<"They multiply out to "<< x * y <<"\n";
cin.get();
cout<< x <<endl;
}
cin.get();
}
int mult ( int x, int y )
{
return x * y;
}
Heh, I know this is pretty jacked now.. But its after I've been tinkering with it for about 30 minutes and I've probably done way way more damage than good. any help is appreciated... I got a few other simple progs to work but this is kicking my buttox :D
Thanks
-Inao
-
Your for loop is wrong.
Code:
for (variable; check variable to stop; modify variable)
i.e.
for (int i = 0; i <10; i++ )
{//stuff}
You must make sure to initialize the variable to something (and it must be a declared variable. It is typical to declare it in the for loop like I did (and that "i" is only used in the for loop).
In your for loop you have x, which has not been previously declared ( int x; ), so that will not work; and then you try and set it to 0, but you use == and not =.
== checks for equality.
= sets something equal to.
Also, your multiply function looks right but you never invoke/call it?
And that last cout << x is useless.
It is also good programming etiquette to make main return something, 0 for success, even if your code compiles fine without it.
The last line before the closing } in main should be “ return 0; “.
-
Ok! fast reply! thanks, I'm going to go implement the main 0 thing, and add an if statement and play around with that, If I have any more troubles I'll be back!
Thanks a ton!
-
Ok, back, That helped alot! Now I have implemented if statements... This worked just fine until I attempted to use the || statement (or, right?). It says the if statement containing it every time, instead of the specified 1 or 2.
Heres what I mean:
Code:
#include <iostream>
using namespace std;
int mult ( int x, int y );
int main()
{
for (int i = 0; i < 10; i++ ) {
int x;
int y;
cout<<"Input two numbers for multiplication: ";
cin>> x >> y;
cin.ignore();
cout<<"They multiply out to "<< x * y <<"\n";
cin.get();
if ( x * y > 999 ) {
cout<<"Aha! Testing my abilties are you?\n";
}
else if ( x * y < 999 ) {
cout<<"Pshh! Too easy!\n";
}
else if ( x * y == 999 ) {
cout<<"What are you trying to pull? This message is a 1/infinty chance!\n";
}
if ( x * y == 1||2) {
cout<<"By the way, You should have known that one!\n";
}
}
cin.get();
return 0;
}
int mult ( int x, int y )
{
return x * y;
}
Thanks for the help!
-
Like this:
Code:
if ( x * y == 1 || x * y == 2)
-
Oh! That makes sense! Thank you again... This is making the whole "learning without a class" thing go alot easier. Also, one more question, Inbetween problems, you have to hit enter to make it restart.... How do I eliminate the enter, or pause? Rem cin.get?
-
-
Ok, I have typed in comments beside all of my code, so I have it as a reference for future code. If these comments are incorrect can you fix them and post back? That would be great:)
Here it is currently:
Code:
#include <iostream> //To use the function iostream.
using namespace std; //To use specific commands from the function.
int mult ( int x, int y ); //Defining mult
int main()
{
for (int i = 0; i < 10; i++ ) {// To make a for loop, see lesson 3
int x;//Declaring variables
int y;
cout<<"Input two numbers for multiplication: ";//What to say!
cin>> x >> y;//To tell it to wait on the user to input two integers
cin.ignore();//To tell the program to ignore ()
cout<<"They multiply out to "<< x * y <<"\n";//So it will return the answer
if ( x * y > 999 ) {//If the returned variable is > 999, do following
cout<<"Aha! Testing my abilties are you?\n";
}
else if ( x * y < 999 ) {//If the returned variable is < 999, do following
cout<<"Pshh! Too easy!\n";
}
else if ( x * y == 999 ) {//If the returned variable is = 999, do following
cout<<"What are you trying to pull? This message is a 1/infinty chance!\n";
}
if ( x * y > 1 && x * y < 10 ) {//If the returned variable is > 1 & < 10 do next
cout<<"By the way, You should have known that one!\n";
}
}//To close the main code block
cin.get();//To require the use to press enter to continue
return 0;
}
int mult ( int x, int y )
{
return x * y;//To tell it to return x * y when called upon
}
Thanks!
-
Code:
#include <iostream> // There is no function iostream() that I'm aware of. :) Be careful with terminology
using namespace std; // Say that all classes, objects, and functions in the scope of the std namespace
// can be used in the program
int mult ( int x, int y ); // Prototyping not defining
I also found it humorous that you defined a function that you never used in your program. :p
Try this:
Code:
cout<<"They multiply out to "<< mult(x,y) <<"\n";
-
I changed/fixed a few things.
The biggest notice is you will notice it is way way easier to read because I changed spacing.
The following:
Is exactly the same to the compiler as:
Code:
cout << "test";
//and the same for
cout << "test" ;
Use as many spaces, tabs and lines as you need to to make your code readable and easy to follow. And I personally always put { and } on lines all by them selves.
Code:
#include <iostream> //To use the library iostream.
using namespace std; //Too much to explain in such little space!
//http://www.cplusplus.com/doc/tutorial/namespaces.html
int mult ( int x, int y ); //function decleration
int main()
{
for (int i = 0; i < 10; i++ ) // To make a for loop, see lesson 3
{
int x;//Declaring variables
int y;
cout << "Input two numbers for multiplication: ";//Prompt user
cin> > x >> y;//To get input
cin.ignore();//To tell the program to ignore any 'extra' whitespace or return char
cout << "They multiply out to " << (x * y) << "\n";//Outputs to screen (and does math)
if ( x * y > 999 ) //If the returned variable is > 999, do following
{
cout<<"Aha! Testing my abilties are you?\n";
}//End if
else if ( x * y < 999 ) //If the returned variable is < 999, do following
{
cout<<"Pshh! Too easy!\n";
}//End else-if
else if ( x * y == 999 ) //If the returned variable is = 999, do following
{
cout<<"What are you trying to pull? This message is a 1/infinty chance!\n";
}
if ( x * y > 1 && x * y < 10 ) //If the returned variable is > 1 & < 10 do following
{
cout << "By the way, You should have known that one!\n";
}
}//End of for-loop
cin.get();//To require the use to press enter to continue
return 0;
}//End of main
int mult ( int x, int y ) //Something I made, and works right but I never used once in my code!
{
return x * y;//To tell it to return x * y when called upon
}
-
Awesome, It makes it alot easier! Thank you thank you ------- I have another question (Sorry!). I use Dev C++ compiler, and I don't generally use the "compile" "Run" and "Compile & Run" buttons, to test and compile my progs... How do I save it where people that don't have Dev C++ to run it? Because I am wanting to send it to my friend, she is wanting to know what I've been working so hard on. Any help would be greatly appreciated, and again, thanks for your previous help all
-
By "don't have Dev C++" do you mean that they don't have a compiler or that they just don't have Dev C++?
If they have a compiler, then that code is completely portable to any modern C++ compiler on Windows. If they don't have a compiler, then you'll have to send them an executable. If you want to send them the source code then you could just copy your code into wordpad and send that as well.
-
How do You make it a an executable?
-
You compile it. :)
Then in the same folder as the source code you should have an exectutable of the same name. :) That's what the DOS console is running when you compile.
-
Can you give me an example?
-
Sure. Paste your code in Dev-C++. Go to Execute > Compile. If it prompts you for a directory, then choose one, if it doesn't it's in the folder that the .cpp is saved in. After you compile go to that folder. The icon should look like a little console window.
-
And thats the executable?--- When I click on those it opens up dev c++, not the actual program on MSDOS
will it be different for someone without it on their comp?
-
No you're clicking on the source code. I'll give you a picture explaination.
http://img380.imageshack.us/my.php?image=compile9cd.jpg
-
oh! I found it! I feel very stupid, sorry for taking so much time up with such a question, and thank you for putting up with me
-
You are going to start to run into a lot of problems; some that even the next day you will consider “trivial” and feel silly for not knowing. I am NOT saying stop asking question; that is what this is for.
The tutorials are nice, good for quick referencing for syntax, ect. But really, you are missing so much background data and info by just using them. I would highly highly recommend getting a book (way more complete and goes into much more detail). I think you will find it easier, you will learn more and understand what you are doing on a more deeper level. Remembering syntax will never make you a good programmer, but understanding what is going on will.
I personally think this book is great!:
http://www.amazon.com/gp/product/032...lance&n=283155
But other people here recommend many others.
Just use the forum search for book….you will get so many results and lot’s of good suggestions for books!
Plus, libraries = free books!
But do not hesitate to keep asking questions.
-
Ok, so that book would be appropriate for a my current skill level? (pretty much none)
-
That book is for people with no skill level / knowledge and ends with the ability to work as a programmer in the real world (for C++). But there are lots of books like that, and each has a different style; which is why libraries rock.
-
Small town= Small library, but I'll have to check out my local one and see if they have a few, perhaps I can print hte titles on here of what I find , or mayb ejust look for ratings through google.. hmm good idea though
-
Small town maybe; but usually entire regions if not whole states libraries are linked and online in all US states. You should search for your local/regional library system online and search the catalogue that way for C++ books.
Also, libraries are for the public, just request a book. It might take a while depending on the time and budget. But I have worked at a library before, and where I worked the budget set-aside for requests was never ever fully used so it just rolled over quarterly to the general budget.