I am new to programming

This is a discussion on I am new to programming within the C++ Programming forums, part of the General Programming Boards category; what i am trying to do with this code is just to get it to count from 1 number the ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Jesup, Georgia, United States
    Posts
    7

    I am new to programming

    what i am trying to do with this code is just to get it to count from 1 number the user inputs to a second number they input and count from one to the other but it only works if the first number is less then the second any tips?

    yes i do realize this is a pretty boring program with no real application but its a learning process
    Code:
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    int main()
    {
    int i , n1, n2;
    cout<< " enter a number and press enter: ";
    cin>> n1;
    cout<< "enter another number and press enter: ";
    cin>> n2;
    i = n1;
    if(n1 < n2){ 
    while (i <= n2) { 
    cout<< i <<' ';
    i = i + 1;
    }
    }
    else{
    while (i <= n2) {
    cout<< i << ' ';
    i = i - 1;
    }
    } 
      system("PAUSE");    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    In your second while loop, i is already bigger than n2. So you could switch to:
    while (n2 <= i) {

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    Jesup, Georgia, United States
    Posts
    7
    thank you that worked

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Indent your code next time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-11-2011, 03:25 PM
  2. Replies: 1
    Last Post: 08-19-2007, 03:55 AM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 10:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

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