i am a very early beginner and am now learning about functions. as an excercise i created this. i think its pretty straight forward but my computer is telling me i havent initialized variable d. not really sure why. besides this message it runs fine. if you could proofread that would be awesome.
// Testing.cpp : while loop in function and then displayed
//
#include "stdafx.h"
#include <string>
#include <iostream>
usingnamespace std;
int countdown (int a) //my function
{
int d;
while(a>0){ // simple while loop that should countdown to 0
cout << a << " ";
a--;
if(a==0){ // here i had it break at zero so i could set d to a value
break;
}
}
a=d;
return (d);
}
int main () // meat of the program
{
int e;
cout << "initiate countdown:";
cin >> e; // simple input/output
cout << countdown(e); // (i thought this was pretty cool :). )
int wait;
cin >> wait; // i know it's not concise but heck it works and this is only for practice
return 0;
}
@Danny Toledo ok i think i know what youre saying. im not 100% but i think youre saying that zero even though its kinda a value its not really because at the end of the function it really says return 0; since d = 0. so i came up with this but i had the same error message.
// Testing.cpp : while loop in function and then displayed
//
#include "stdafx.h"
#include <string>
#include <iostream>
usingnamespace std;
int countdown (int a) //my function
{
int d;
while(a>0){ // simple while loop that should countdown to 0
cout << a << " ";
a--;
if(a==1){ // now its breaking at 1 and displaying a in the if so a has a value
cout << a;
break;
}
}
a=d;
return (d);
}
int main () // meat of the program
{
int e;
cout << "initiate countdown:";
cin >> e; // simple input/output
cout << countdown (e); // (i thought this was pretty cool :). )
int wait;
cin >> wait; // i know it's not concise but heck it works and this is only for practice
return 0;
}
when i tried your thing TheMeerkat i actually had more trouble. plus i think this way is pretty cool and doesnt seem to cause any trouble. at least not with a function this simple
Yea OK, I just have never saw it done like that (not that I've been programming very long). I take it what you expect that line to do is to output the return value from the countdown function?
Actually, it seems d doesn't really equal anything. Nowhere is d given a value. In the 'countdown' function, f1ddl3r has a to equal d, which could actually contain anything since it was not initialized, but returns d. Shouldn't it be d=a;? Or better yet, bypass that part, and just have return (a) since d isn't really needed.
right so i did have them switched ok. ill try reuturn (a) too that looks pretty swell and makes a lot of sense. and ill change that loop. good call Nisheeth
@f1ddl3r:
Don't return (a). Let it remain as it is. Otherwise there is no point in making d.
The error you were having was a = d means set a's value to be equal to d (in short, a is equal to d) But as d has no value at the moment:
it will mean a is equal to no value
The compiler will set the value of the right variable to the left one.
another example of the same case just elaborated will be: 5+6 = a;
is mathematically a correct statement.
It is not so in programming.
This would mean set the value of a to that of 11 (5+6), which is meaningless.
So a = 5+6 is correct as it will mean set 11 to be the value of a.
right ok great thanks. yeah as a beginner its really hard to keep straight what goes left to right and what goes right to left and i know that practice is what will really get it to click. thanks!
@ Nisheeth "Don't return (a). Let it remain as it is. Otherwise there is no point in making d."
-> Indeed, so do "return a" and get rid of d. If there is no point to having a variable, getting rid of it makes a lot more sense than giving it some useless job.
Also, "(in short, a is equal to d)": that's just confusing him now. You should explain it this way:
a = b; --> "Set a's value equal to b's current value."
a == b; --> "Is a's value equal to b's value?", thus "if(a == b)" --> "If a is equal to b".
Your examples are a perfect way to make this clear though, but for those who like/need to say it in words (it's how I check my logic), properly distinguishing the two is necessary.
Many programmers use a countdown like this to save using a variable and streamline abit:
1 2 3 4 5 6 7 8 9 10 11 12
int countdown (int &a) //my function
{
/* deincrement 'a' by 1 each loop and
continue while true (not equal to 0) */
while(--a)
{
cout << a << " ";
}
return a;
}
awesome! its working swell now thanks everyone. ill keep that saved somewhere carebearboy but for now its really just for excercise and im not too worried about the technics right now. thanks anyway. my final code is this:
// Testing.cpp : while loop in function and then displayed
//
#include "stdafx.h"
#include <string>
#include <iostream>
usingnamespace std;
int countdown (int a) //my function
{
while(a>0){ // simple while loop that should countdown to 0
cout << a << " ";
a--;
}
return (a);
}
int main () // meat of the program
{
int e;
cout << "initiate countdown:\n";
cin >> e; // simple input/output
while(e>=0){ //stuck it in a loop so that the program does not finish after one run
cout << countdown (e); // (i thought this was pretty cool :). )
cout << "\n\nrepeat coundown, new integer:\n";
cin >> e;
}
int wait;
cin >> wait; // i know it's not concise but heck it works and this is only for practice
return 0;
}