From your original post:
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.a program with a function printmsg() that has a parameter n. the function prints the messag "hello world" n time
I don't see any parameter or loop for this function.Code:void printmsg() { cout<<"hello world"; }
Jim
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.
If you can not,just state it and i am going to explain it to you right away!Code:#include <iostream> using namespace std; main() { int a=7; cout<<a++<<endl; cout<<a<<endl; cout<<--a<<endl; cout<<a<<endl; return 0; }
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); }
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
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
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 --)
or with less linesCode:#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); }
Code:#include<iostream> using namespace std; main() { int n; cout<<"Enter a number : "; cin>>n; while(n--) { cout<<"hello world"<<endl; } return(0); }
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
What this line does is this.Declare an integer i and set it to zero(int i=0).Code:... for(int i=0 ; i<=10 ; i++) { cout<<"i am into the loop and it feels good"<<endl; } ...
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); }
"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.
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