Intro + help

This is a discussion on Intro + help within the C++ Programming forums, part of the General Programming Boards category; Hey all, i'm new to the whole programming scene. I came to this site to learn the basics of C++ ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    Intro + help

    Hey all, i'm new to the whole programming scene. I came to this site to learn the basics of C++ so I can have a better understanding of programming language so that I may later apply these skills to UT2004 and soon UT3.

    Although I had scucessfully made some mutators in the past, making them took a very long time and by the time I was done I look back at the poorly put together code and still wonder what some of it means.

    Anyhow, I am having a problem with this counting program that I threw together... This is driving me crazy. The only way i can get this piece of code to work is to put it inside the FOR loop. otherwise if I place it as a seperate IF statement, the program will completley pass it up. I will post the entire code for the program.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main () {
    
      int m;
      int specified;
      int n;
    
      cout<<"Pick a number 1-50 : ";
      cin>> specified;
      cin.ignore();
      cout<<"Pick method of sorting : \n ==================== \n 1 : multiple rows\n 2 : Single row \n ==================== \n \n";
      cin>> m;
      cin.ignore();
    
      if ( m == 1 ) {
        for (int n = 0; n < specified + 1; n++) {
          cout<< n <<endl;
        }
      }
    
      else if ( m == 2 ) {
        for (int n = 0; n < specified + 1; n++) {
          cout<< n <<", ";
        }
      }
    
      else {
        cout<<"bad input";
      }
      
      if ( n == specified ) {
        cout<< n <<"... finished!";
      }
      return 0;
    }
    This works but not the way I intended. I wish to append "... finished!" to the end of the counting sequence instead of another comma.

    This works, but I was told by a friend that it is a redundant check and could be done better.

    Code:
      else if ( m == 2 ) {
        for (int n = 0; n < specified + 1; n++) {
          cout<< n <<", ";
          
            if ( n == specified ) {
                cout<< n <<"... finished!";
            }
        }
      }
    Some answers why this does not work outside the FOR loop will clear up alot of confusion... This same problem plagued me in U script...
    Last edited by D2k204; 07-29-2007 at 05:28 AM.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    In your original code, 'n' is undefined here.
    Code:
      if ( n == specified ) {
        cout<< n <<"... finished!";
      }
    in other words, you haven't assigned it a value, so it could be anything

    Your previous loop has not modified 'n', instead it has created a new variable, in a different scope, which also happens to be called 'n'
    Code:
        for (int n = 0; n < specified + 1; n++) {
          cout<< n <<endl;
        }
    To avoid confusion, I would suggest giving this variable a different name, or, if you're actually intending to update the 'outer' variable n, remove the int keyword from the loop.

    Also, be aware that your loop is counting to specified+1 - so the final value of 'n' will be specified+1

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    920
    Also, be aware that your loop is counting to specified+1 - so the final value of 'n' will be specified+1
    The final value will be specified. Replacing "< var + 1" might be clearer as "<= var".
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    Thanks for the replies guys, I really appriceate it. Anyhow, I have been experimenting with the beginners tutorial knowledge trying to make the program a bit more complex and support a few extra options. With alot of trial and error, this is what came out of the "counting program."

    Compiler that was used was Code::Blocks on Win XP... Although I tihnk it should work on just about anything...

    Anyways If you guys can help me out, please look over the code and post suggestions of how I might make more efficent use of the code if possible.

    Again, thank you guys for your time replying, it really helped out.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by CodeMonkey View Post
    The final value will be specified. Replacing "< var + 1" might be clearer as "<= var".
    I think you're getting confused between the final value of n and the value of n during the final iteration of the loop (Which are both different things). The loop is terminated when 'n's value causes the loop condition to yield false - this will happen when 'n' is incremented, after the final iteration, to specified+1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Intro to embedded programming
    By ohanna in forum C Programming
    Replies: 6
    Last Post: 03-13-2008, 04:16 PM
  2. New to C#? Intro Lesson
    By oval in forum C# Programming
    Replies: 0
    Last Post: 04-07-2006, 05:57 PM
  3. OnExit() and Intro button
    By artelo in forum Windows Programming
    Replies: 0
    Last Post: 09-24-2003, 03:13 PM
  4. Beginer looking for an intro to graphics in C++...
    By scooby13q in forum C++ Programming
    Replies: 17
    Last Post: 06-07-2002, 06:19 PM
  5. VC++ Intro edition.
    By Doble04 in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2001, 06:16 PM

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