Function Loop problem

This is a discussion on Function Loop problem within the C++ Programming forums, part of the General Programming Boards category; a program with a function printmsg() that has a parameter n . the function prints the messag "hello world" n ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    33

    Function Loop problem

    a program with a function printmsg() that has a parameter n. the function prints the messag "hello world" n time
    tried making one. not looping. what is problem
    Code:
    #include<iostream.h>
    #include<conio.h>
    int getsum(int a);
    void printmsg();
    main()
    {
        clrscr();
        int n;
        cout<<"Enter a number : ";
        cin>>n;
        do
        {
            printmsg();
            cout<<getsum(n);
        }
        while(getsum(n)<=1);
        getch();
        return(0);
    }
    int getsum(int a)
    {
        int sum;
        do
        {
            sum=a--;
            return sum;
        }
        while(a<=1);
    }
    void printmsg()
    {
        cout<<"hello world";
    }

  2. #2
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    200
    I have more questions than answering the problem. How is that compiling? At all?

    iostream.h shouldn't exist.
    #include <iostream> would be the correct nomenclature.

    main() has no type. You never even include std::cout or std::cin, so that shouldn't let you do anything.

    I suggest getting a new compiler first o3o.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    function getsum does not do anything special.It returns immediately variable sum(in essence a).The -- is done at a after the assignment of a to sum,so it does not affect at all.So if i input 10,getsum will return 10,and 10 is not <= of 1,so false statement at line 16,so no looping!

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Rodaxoleaux View Post
    I have more questions than answering the problem. How is that compiling? At all?

    iostream.h shouldn't exist.
    #include <iostream> would be the correct nomenclature.

    main() has no type. You never even include std::cout or std::cin, so that shouldn't let you do anything.

    I suggest getting a new compiler first o3o.
    Yes it compiles.main has no type,but by default it is equivalent to write main() to int main(void).iostream can be written like this.Fot the std the only explanation is that conio.h may contain a using namespace std.(however i do not know what conio.h is)

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    im using turbo c++ 3.0
    im a noob at programming and my teacher taught me this way of writing a program through C++.

    #include<iostream.h> works for some reason in my compiler
    i dont know about your std::cout or std::cin but my cout and cin work which my teacher taught me works.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Actually in C++ default return types are not permitted, it shouldn't compile with a standard conforming compiler. The <iostream.h> header file is a pre-standard header file that did not use the standard namespace. This means that the OP is using an ancient compiler and desperately needs to find a more modern compiler. Also the <conio.h> header is a non-standard DOS/Windows include file and it doesn't know anything about the standard namespace.

    To the OP, You need to find a new compiler that one is several decades old and is very very outdated. You will find, if you insist on continuing to use that compiler, that you will find quite a few things that don't seem to work correctly. This is because the C++ that compiler uses is so old that many of it's functions are no longer valid in the current C++ world.

    Jim
    Last edited by jimblumberg; 07-12-2012 at 09:52 AM.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Of course #include<iostream.h> works!!!!But we use to write it as #include<iostream>...the .h reminds me of C.
    Now about the loop,do you understand what i said above?

    EDIT -> Some warnings should the compiler shows to you though
    Last edited by std10093; 07-12-2012 at 09:53 AM.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    Quote Originally Posted by jimblumberg View Post
    Actually in C++ default return types are not permitted, it shouldn't compile with a standard conforming compiler. The <iostream.h> header file is a pre-standard header file that did not use the standard namespace. This means that the OP is using an ancient compiler and desperately needs to find a more modern compiler. Also the <conio.h> header is a non-standard DOS/Windows include file and it doesn't know anything about the standard namespace.

    Jim
    i think this is an ANCIENT (turbo c++ 3.0) compiler which i heard from my teacher or some online forums whatnot. why does my school use ancient compilers ahhhhhhhh.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I suggest you to focus on the method you are trying to loop for now....

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    Quote Originally Posted by std10093 View Post
    Of course #include<iostream.h> works!!!!But we use to write it as #include<iostream>...the .h reminds me of C.
    Now about the loop,do you understand what i said above?

    EDIT -> Some warnings should the compiler shows to you though
    uhmm, i get that return sum returns the variable, thats all i get. all the others are just confusing to me.

  11. #11
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    200
    Again with people forcing students to use Turbo C++... I'm starting school this year. I've been hoping that I would be using a C++11 compliant compiler. Now I'm just praying that I can use a compiler that was made after I was born o.e
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    i think this is an ANCIENT (turbo c++ 3.0) compiler which i heard from my teacher or some online forums whatnot. why does my school use ancient compilers ahhhhhhhh.
    That's a good question, why don't you ask them? They are doing you a great disservice by insisting you use this relic.

    Jim

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    thanks std10039.

    i shall carefully analyze your sentence and understand it to why my program does not work.

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by langamer101 View Post
    thanks std10039.

    i shall carefully analyze your sentence and understand it to why my program does not work.
    Ok thought of it carefully.For anything you do not understand,do not hesitate to ask

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    Quote Originally Posted by jimblumberg View Post
    That's a good question, why don't you ask them? They are doing you a great disservice by insisting you use this relic.

    Jim
    ok lol, ill try asking my teacher why the heck do i a student have to use an ancient c++ compiler which is outdated and not like the rest which uses a modern c++ compiler, which i found out recently in a c++ forum. interesting.

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

Similar Threads

  1. problem with while loop using getch function
    By youngbee2 in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2012, 09:39 AM
  2. function with loop within a loop
    By rabert1 in forum C Programming
    Replies: 7
    Last Post: 04-17-2012, 02:30 AM
  3. function, function call, for loop
    By jackson6612 in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2011, 05:03 PM
  4. function loop help
    By akahizzle in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2011, 11:15 AM
  5. Problem with for loop calling external function
    By lordbubonicus in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 11:54 AM

Tags for this Thread


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