What is wrong here, just won't print the correct numbers

This is a discussion on What is wrong here, just won't print the correct numbers within the C++ Programming forums, part of the General Programming Boards category; #include <iostream.h> #include <stdlib.h> int main() { for(int x=1; x<=10; x++) { int n_ = 1; //1! = 1 n_ ...

  1. #1
    Unregistered
    Guest

    Arrow What is wrong here, just won't print the correct numbers

    #include <iostream.h>
    #include <stdlib.h>

    int main()
    {

    for(int x=1; x<=10; x++)
    {

    int n_ = 1;
    //1! = 1

    n_ = ((n_*x)-1)*x;
    //2! = 1! *2
    //3! = 2! *3
    //4! = 3! *4

    std::cout << n_ << " ";
    }




    system("PAUSE");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    49
    What do you want? I cannot understand.

    an error: please change :
    Code:
    #include <iostream.h>
    to
    Code:
    #include <iostream>
    using namespace std; // Edited
    Hello, everyone.

  3. #3
    Unregistered
    Guest
    what numbers to you expect, and what numbers are printed out?

    form code I expect to see 0 2 6 12 20 ....

  4. #4
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    1. You are using older header files so you don't have to name the namespace for cout. Get rid of the std:: in front of cout.

    Ignore my 2 and 3 I didn't see the last part down there it's tooo early.

    That should get you started. Keep working. Have a nice day.
    Last edited by DISGUISED; 05-10-2002 at 10:30 AM.

  5. #5
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    WTH? Why would you remove the .h? Your compiler may be different?

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    49
    Code:
    #include <iostream>
    using namespace std;
    
    void main()
    {
        int nResult = 1; // 1!=1
        for(int i=2; i<=10; i++)
        {
            nResult *= i;
            cout << nResult << "\t";
        }
    }
    Hello, everyone.

  7. #7
    Unregistered
    Guest
    if you use std::cout, that implies you are using namespace standard. All compilers I know that use namespaces also use the convention that files listed in the #include section don't use the .h extension. If your compiler requires you to use the .h extension when listing iostream then it probably doesn't use namespaces and listing the namespace will cause an error. So, it's probably one or the other, iostream alone with namespace std, or iostream.h without namespace standard.

    since n_ is assigned the value of 1 each time through the loop, the expression (n_*x) is the same as x so you end up with the expression (x - 1)*x. Whether that is as intended or not is not clear.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    49
    All C++ standard header files have no .h ; .h is old C style; A C++ programmer should use C++ standard.
    Hello, everyone.

  9. #9
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb

    [quote]
    Code:
    int main() 
    { 
    
    for(int x=1; x<=10; x++) 
    { 
    
    int n_ = 1;
    [quote]

    Considering this Board. C-PROGRAMMING. C++ methods are in the way. C would never allow u to declare any variable after u have started writing the body of a function. U should do it before the code part.

    -Sriharsha.
    Help everyone you can

  10. #10
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Unhappy

    Sorry Sorry Sorry.

    I just did not pay heed to the board. I justtyped new threads and I just did not look at the board...

    -Sriharsha.
    Help everyone you can

  11. #11
    Unregistered
    Guest
    I want to print:


    2! = 1!*2
    2! = 2
    3! = 2!*3
    3! = 6
    4! = 3!*4
    4! = 24
    5! = 4!*5
    5! = 120
    6! = 5!*6
    6! = 720
    7! = 6!*7
    7! = 5040

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    49
    Code:
    #include <iostream>
    using namespace std;
    
    void main()
    {
        int nResult = 1; // 1!=1
        for(int i=2; i<=10; i++)
        {
            nResult *= i;
            cout << i << "! = " << i-1 << "! *" << i << endl;
            cout << i << "i = " << nResult << endl;
        }
    }
    Hello, everyone.

  13. #13
    Unregistered
    Guest
    many a compiler is available that doesn't comply with the current standards. Unless you want to be another MS and demand that we all "update" as soon as the newest and "greatest" becomes available, taking "older" compilers into account is still necessary. In fact, I suspect that there are many more "non-compliant" compilers in use at this time than compliant ones.

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    49
    The code I listed can run on VC6 / BCB / BC++ / GNU C++ .
    Hello, everyone.

  15. #15
    Unregistered
    Guest
    VC++ 6 and BCB 5, but not earlier versions. I don't know about others here, but I not about to buy the latest and greatest when what I have suits my needs. So I'll stick with what I have, be aware of the newer techniques, and be cognizant that many of us can't use them.

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

Similar Threads

  1. how to print only N numbers of string
    By umen242 in forum C Programming
    Replies: 1
    Last Post: 06-26-2008, 01:55 AM
  2. How to print numbers?????
    By AssistMe in forum C Programming
    Replies: 2
    Last Post: 03-04-2005, 08:22 AM
  3. Print binary numbers to disk file, problem
    By Guti14 in forum C Programming
    Replies: 4
    Last Post: 10-04-2004, 08:33 AM
  4. Replies: 1
    Last Post: 05-01-2003, 03:52 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM

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