Newb question

This is a discussion on Newb question within the C++ Programming forums, part of the General Programming Boards category; I'm really new, really really new, to C++, and all programming for that matter. I just picked it up as ...

  1. #1
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32

    Question 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

    Thanks
    -Inao

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    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; “.
    Last edited by Enahs; 11-29-2005 at 08:44 PM.

  3. #3
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    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!

  4. #4
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    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!

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Like this:

    Code:
     if ( x * y == 1 || x * y == 2)
    Sent from my iPad®

  6. #6
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    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?

  7. #7
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Rem cin.get?
    Yes.

  8. #8
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    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!

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    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.

    Try this:
    Code:
    cout<<"They multiply out to "<< mult(x,y) <<"\n";
    Last edited by SlyMaelstrom; 11-30-2005 at 06:14 PM.
    Sent from my iPad®

  10. #10
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    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:
    Code:
    cout<<"test";
    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
    }

  11. #11
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    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
    The Pristine Angel lives on!

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    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.
    Sent from my iPad®

  13. #13
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    How do You make it a an executable?
    The Pristine Angel lives on!

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    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.
    Sent from my iPad®

  15. #15
    Apprentice to Sly & Shane Inao's Avatar
    Join Date
    Nov 2005
    Location
    Oklahoma
    Posts
    32
    Can you give me an example?
    The Pristine Angel lives on!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  3. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  4. newb question
    By Jan79 in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2003, 09:59 AM
  5. Win app, newb question
    By Blender in forum Windows Programming
    Replies: 9
    Last Post: 02-04-2003, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21