Thread: Function Loop problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    203
    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
    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)

  4. #4
    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.

  5. #5
    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 08:53 AM.

  6. #6
    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.

  7. #7
    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!

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    Quote Originally Posted by std10093 View Post
    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!
    i think i get the uhm -- thing.
    here's a sample
    Code:
    a--
    sum=a
    return sum;
    so i must use -- first before assigning it to sum? correct?.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You have to understand that the location of ++ and -- is significant.
    Check this example and try to explain why the output is what it is.
    Code:
    #include <iostream>
    using namespace std;
     
    
    main()
    {
        int a=7;
        cout<<a++<<endl;
        cout<<a<<endl;
        cout<<--a<<endl;
        cout<<a<<endl;
        
        return 0;
    }
    If you can not,just state it and i am going to explain it to you right away!

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    33
    whats endl?

    i did a major revision of my code
    i dont know if this is correct or not. plss help with the do while.
    Code:
    #include<iostream.h>
    #include<conio.h>
    int prnmsg(int a);
    main()
    {
        clrscr();
        int n;
        cout<<"Enter a number : ";
        cin>>n;
        do
        {
            cout<<"hello world"<<prnmsg(n);
        }
        while(prnmsg(n)<=1);
        getch();
        return(0);
    }
    int prnmsg(int a)
    {
        int sum;
        do
        {
            a--;
            sum=a;
            return sum;
        }
        while(a<=1);
    }

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    endl says to end the line at the ouput(like you hitted enter)..Nothing special

    As for the code you wrote,it is a bit better,but not good enough.The goal is to print "hello word" as many times as the n.Correct?Your program does not do that

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by langamer101 View Post
    Code:
    #include<iostream.h>
    #include<conio.h>
    int prnmsg(int a);
    main()
    {
        clrscr();
        int n;
        cout<<"Enter a number : ";
        cin>>n;
        do
        {
            cout<<"hello world"<<prnmsg(n);
        }
        while(prnmsg(n)<=1);
        getch();
        return(0);
    }
    int prnmsg(int a)
    {
        int sum;
        do
        {
            a--;
            sum=a;
            return sum;
        }
        while(a<=1);
    }
    Let's see what does this code do..
    Until line 9 is pretty clear.
    Line 10 we reach the do while structure.We enter the loop and the message is printed.
    Then ,in line 14,the prnmsg function is called.Inside this function we decrease the a(which has the value of n) by one in line 23.
    Then we assign a to sum at line 24.
    Then ,in line 25, the function will be terminated(!) and sum will be returned.So the do-while structure at lines 21-27 is useless,because at line 21 we are sure that our code will enter the loop,but in line 25 the function will be terminated,so the statement at line 27 with the while will not be checked at all.No matter if a<=1(line 27) <-the function is not going to reach this point,so the loop inside the function will never be executed!!
    Then after the function returns sum we come back at line 14 and check if prnmsg(n)<=1 which is equivalent to sum<=1.This will be true only if the user inputs something that is smaller or equal to 1,so actually <= is wrong.

    You could use something like this
    (read the e.g. i have on a post above with --)
    Code:
    #include<iostream>
    using namespace std;
    
    int main(void)
    {
     
        int n;
        cout<<"Enter a number : ";
        cin>>n;
        do
        {
            cout<<"hello world"<<endl;
        }
        while(--n);
        
        return(0);
    }
    or with less lines

    Code:
     
    #include<iostream>
    using namespace std;
    main()
    {
     
        int n;
        cout<<"Enter a number : ";
        cin>>n;
        while(n--)
        {
            cout<<"hello world"<<endl;
        }
        
        return(0);
    }

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by langamer101 View Post
    . plss help with the do while.
    Why don't you try a for structure instead of do while.A for structure is used when we know the excact times of loops that are going to be made.It is written like this
    Code:
    ...
    for(int i=0 ; i<=10 ; i++)
    {
          cout<<"i am into the loop and it feels good"<<endl;
    }
    ...
    What this line does is this.Declare an integer i and set it to zero(int i=0).
    Then it is going to check if the statement is true or false(i<=10).If it is true the loop will be executed,if it false it will skip the loop and continue into the rest of the code.
    In case we enter the loop when the loop reaches to it's end and so we go back to line 2,where the line of for lies.There we are going to increase i by one factor(i++).
    So how many times will this loop be executed?Eleven!At the last loop i is 10,it enters the loop,prints the message at line 4 and then at the end of the loop it is increased by one,so it becomes 11.So when we go back at line 2,we check if i<=10,but i is 11,so false statement,so we skip the loop and go on at line 6.

    So when should i use a for structure instead of a while or a do-while structure?
    for structure is used when we know the excact numbers the loop will be executed.At your program we want to print hello world n times.You do not how many times the message will be printed(that is to say,how many times the loop is going to be executed),until the user inputs a value for n.So,you do not know if it is going to be 10 times or 3 or whatever.But you know that it is going to be printed n times,and that is enough for you to decide to use a for statement
    So what we should write is this
    Code:
    #include <iostream>
    using namespace std;
    
    main()
    {
     
        int n;
        cout<<"Enter a number : ";
        cin>>n;
        for(int i=0; i<n ; i++)
        {
            cout<<"hello world"<<endl;
        }
        
        return(0);
    }

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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 08:52 AM.

  15. #15
    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.

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, 08:39 AM
  2. function with loop within a loop
    By rabert1 in forum C Programming
    Replies: 7
    Last Post: 04-17-2012, 01:30 AM
  3. function, function call, for loop
    By jackson6612 in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2011, 04: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, 10:54 AM

Tags for this Thread