Altered counter

This is a discussion on Altered counter within the C++ Programming forums, part of the General Programming Boards category; #include <iostream.h> #include <stdlib.h> #include <conio.h> int main() { int num2; cin >> num2; { for(int num2;num2<9999999;num2++) { cout<<num2<<endl; } ...

  1. #1
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32

    Altered counter

    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    int main()
    {
    int num2;
    cin >> num2;
    {
    for(int num2;num2<9999999;num2++)
    {
    cout<<num2<<endl;
    }
    return 0;
    }

    I took the source for a counter from the site tutorial, and then manipulated it so it would count higher, and the user chose the starting number. Why doesn't it work? My compiler won't execute it because of "parse error at end of input", but I can't find anything.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    You declare num2 twice. Instead, in your loop try doing

    for(int num3=num2; num3<9999999; num3++)
    {cout<<num3<<endl;}

  3. #3
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Didn't help.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    O, my bad. You have an extra bracket in there after the input.

  5. #5
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    #include <conio.h> 
    int main() 
    { 
       double num2; 
       cin >> num2; 
       for(int num3 = num2;num3<9999999;num3++) 
       { 
            cout<<num3<<endl; 
       } 
        return 0; 
    }
    if you want it to go all the way up to 9999999, num2 should be defined as double. and truly you can do this:

    Code:
    for(int num3 = num2;num3<9999999;num3++)  
            cout<<num3<<endl;
    Last edited by xlnk; 03-31-2002 at 07:57 PM.
    the best things in life are simple.

  6. #6
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Thanks. Works now. I am just adding a choice to do negative, etc., so I might need more help.

  7. #7
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    int main()
    {
    double num2;
    int num;
    cout << "Please input the starting number: ";
    cin >> num2;
    cout << "1. Positive 2. Negative: ";
    cin >> num;
    if(num==1)
    {
    for(double num3=num2; num3<999999999; num3++)
    {
    cout<<num3<<endl;
    }
    if(num==2)
    {
    for(double num3=num2; num3<999999999; num3--)
    {
    cout<<num3<<endl;
    }
    return 0;
    }

    Why doesn't this work. It says the same error as before.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    145

    Thumbs up Bracket missing

    You are missing end } brackets for both of your if statements.

  9. #9
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Where? Can't find.

  10. #10
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    #include <conio.h> 
    int main() 
    { 
        double num2; 
        int num; 
        cout << "Please input the starting number: "; 
        cin >> num2; 
        cout << "1. Positive 2. Negative: "; 
        cin >> num; 
        if(num==1) 
        { 
             for(double num3=num2; num3<999999999; num3++) 
             { 
                    cout<<num3<<endl; 
             } 
        } 
        if(num==2) 
       { 
            for(double num3=num2; num3<999999999; num3--) 
            { 
                   cout<<num3<<endl; 
            } 
       }
       return 0; 
    }
    I would suggest this:

    Code:
     #include <iostream.h> 
    #include <stdlib.h> 
    #include <conio.h> 
    int main() 
    { 
        double num2; 
        int num; 
        cout << "Please input the starting number: "; 
        cin >> num2; 
        cout << "1. Positive 2. Negative: "; 
        cin >> num; 
        
        switch(num)
        {
              case (num == 1):
              {
                    for(double num3=num2; num3<999999999; num3++) 
                         cout<<num3<<endl; 
                    break;
              }
    
              case (num == 2):
              {
                    for(double num3=num2; num3<999999999; num3--)                     
                         cout<<num3<<endl; 
                    break;
              }
     
              default:
                   cout << "Invalid";
                   break;
         }
                   
       return 0; 
    }
    the best things in life are simple.

  11. #11
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    3 compiler errors:

    case label `(num == 1)' does not reduce to an interger constant

    case label `(num == 2)' does not reduce to an interger constant

    warning: unreachable code at beginning of switch statement.

  12. #12
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    sorry

    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    #include <conio.h> 
    int main() 
    { 
        double num2; 
        int num; 
        cout << "Please input the starting number: "; 
        cin >> num2; 
        cout << "1. Positive 2. Negative: "; 
        cin >> num; 
        
        switch(num)
        {
              case 1:
              {
                    for(double num3=num2; num3<999999999; num3++) 
                         cout<<num3<<endl; 
                    break;
              }
    
              case 2:
              {
                    for(double num3=num2; num3<999999999; num3--)                     
                         cout<<num3<<endl; 
                    break;
              }
     
              default:
                   cout << "Invalid";
                   break;
         }
                   
       return 0; 
    }
    the best things in life are simple.

  13. #13
    Registered User marcusbankuti's Avatar
    Join Date
    Mar 2002
    Posts
    32
    Originally posted by xlnk
    sorry

    Code:
    #include <iostream.h> 
    #include <stdlib.h> 
    #include <conio.h> 
    int main() 
    { 
        double num2; 
        int num; 
        cout << "Please input the starting number: "; 
        cin >> num2; 
        cout << "1. Positive 2. Negative: "; 
        cin >> num; 
        
        switch(num)
        {
              case 1:
              {
                    for(double num3=num2; num3<999999999; num3++) 
                         cout<<num3<<endl; 
                    break;
              }
    
              case 2:
              {
                    for(double num3=num2; num3<999999999; num3--)                     
                         cout<<num3<<endl; 
                    break;
              }
     
              default:
                   cout << "Invalid";
                   break;
         }
                   
       return 0; 
    }
    Thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  2. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 02:17 AM
  3. Flowchart Question
    By dmkanz07 in forum C Programming
    Replies: 1
    Last Post: 04-08-2007, 11:33 AM
  4. Counter Heap Sort
    By Achillles in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 12:17 PM
  5. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 12:58 PM

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