Thread: Function Loop problem

  1. #16
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by langamer101 View Post
    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.
    This is not something unusual.At my university(DIT),compiles for C++ at the ubuntu labs are out of date...

  2. #17
    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?.

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    From your original post:
    a program with a function printmsg() that has a parameter n. the function prints the messag "hello world" n time
    So please show me a function named printmsg() that has a single parameter named n. Also show me where, in this function there is a loop.
    Code:
    void printmsg()
    {
        cout<<"hello world";
    }
    I don't see any parameter or loop for this function.

    Jim

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

  5. #20
    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);
    }

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

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may want to think about a for loop instead of the while, then you don't need to worry about incrementing a inside your loop. Where are you printing the message? Why do you need to sum the number? Where is that in your problem description? You do know that when the program encounters a return statement it immediately returns from the function. Because of this how many times do you think your loop will repeat?


    Jim

  8. #23
    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);
    }

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std10093, while it is good of you to help, at least use standard C++ code.
    main must have a return type, and this is not optional.
    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.

  10. #25
    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);
    }

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std10093, seriously stop posting non-standard C++ code!
    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.

  12. #27
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "for()" and "do-while()" are loops (the keywords "for" and "do-while" are statements). "Structure" has a very different meaning in C/C++. Be careful with your terminology as to not confuse those who are learning.

  13. #28
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Elysia View Post
    std10093, seriously stop posting non-standard C++ code!
    Tell me what's wrong so i can fix it and not do it again The only thing i see is line 4 which i forgot it because of the 1st post of langamer101

    Matticus thanks for the statement

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why don't you use a modern compiler? I'm sure it will know what you're doing wrong.
    And yes, the 4th line is the cluprit because every function must have a return type.
    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.

  15. #30
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I have netbeans,and i install it this year,so i had the idea i got an up to date..
    I did not notice it though.Sorry

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