Stuck on a 0.

So, I'm kinda new to this and i took an extra course in my school to learn programming. Got this task and i tried to write a program to count it, but it keeps telling that the answer is 0.So the task is: on the first day it snowed s millimeters of snow, then the next day it snowed m millimeters more, count how many days (or d) does it take to reach n millimeters(all sizes are natural). if s=17, m=3, n=65, then d=4;
So i wrote this, but it keeps spewing out that d=0. Can anyone tell me where is the problem here ?
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;
int main()
{
int s, m, d=0, n;
cout <<"s: ";cin >> s;
cout <<"m:";cin >> m;
cout <<"n:";cin >> n;
while (s>=n){
d++;
s+=m;
n-=s;
}
cout<<"d:"<< d << endl;
return 0;
}
while (s>=n)

s is 17.
n is 65.
is 17 > = 65?

nope.
does it enter the while loop at all?
nope.
it writes out 0, correctly, as that is d's value!
Last edited on
Just focusing on the code: s = 17, m = 3, n = 65.
Your while loop's condition for looping is "s >= n". 17 is not greater or equal to 65, so the while loop is never entered. Thus, d stays as 0.

Perhaps you meant while s <= n?

(Edit: ninja'd by jonnin :p)
Last edited on
use this: [*code] put your code here [*/code] . without the ' * '.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;
int main()
{
	double s, m, d = 0, n, avg;
	cout << "Enter the first amount (S)\n";
	cin >> s;
	cout << "Enter the secound amount (M)\n";
	cin >> m;
	cout << "Enter the  amount you want to calcualte for (N)\n";
	cin >> n;
	//Ithink its better if u find the average in the first 2 days and then try to find how much it needs more to reach d
	avg = s + m / 2.0;
	d = n / avg;
	cout << "d:" << d << endl;
	system("pause");//theis would pause the screen after u get the output
	return 0;
}

I think this is the true way to use it . doubles instead of int , and using the average .
am learning just like you.
s + m / 2.0 is not taking the average, it need parentheses. Also I interpreted the question as meaning it snows m millimeters every day after the first day, but that could be wrong (your solution makes more realistic sense if only 2 days are known). I'm not sure why OP is doing n -= s in the loop.
Last edited on
I had to actually search for the meaning of parentheses.
you mean something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;
int main()
{
	double s, m, d = 0, n, avg;
	cout << "Enter the first amount (S)\n";
	cin >> s;
	cout << "Enter the secound amount (M)\n";
	cin >> m;
	cout << "Enter the  amount you want to calcualte for (N)\n";
	cin >> n;
	//Ithink its better if u find the average in the first 2 days and then try to find how much it needs more to reach d
	avg = (s + m )/ 2.0;
	d = n / avg;
	cout << "d:" << d << endl;
	system("pause");//theis would pause the screen after u get the output
	return 0;
}
Yep, that's what I meant.
well, i need to use while to get the answer, and to be clearer next day it snows m more than the last, i meant, that is snows not only two days, it can snow more, thus every next day it snows m millimeters more than the last. So i need to find how many days does it snow until s is more than n.
Last edited on
well i fixed it up a bit, but now, the problem is that i entered that s<=n and it shows that it snowed for 3 days, but i need to make it that s>=n, but if i put that, as jonin said, it will not enter the loop. what can i do ?
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;
int main()
{
    int s, m, d=0, n;
    cout <<"s: ";cin >> s; 
    cout <<"m:";cin >> m; 
    cout <<"n:";cin >> n; 
    while(s<=n){
        d++;
        s+=m;
        m+=s;
    }
cout<<"d:"<< d << endl;
return 0;
}
Last edited on
so finnaly my teacher helped me, this is how it was supposed to be like
#include <iostream>


using namespace std;
int main()
{
    int s, m, d=0, n;
    int howmuchitsnowed = 0;
    cout <<"s: ";cin >> s; 
    cout <<"m:";cin >> m; 
    cout <<"n:";cin >> n; 
    while(howmuchitsnowed<n){
        kiekprisnigo+=s;
        d++;
        s+=m;
    }
cout<<"d:"<< d << endl;
return 0;
}
Last edited on
kiekprisnigo is not defined nor initialized .
Looks like kiekprisnigo is supposed to be howmuchitsnowed, probably got butchered during translation or something.
In Esperanto, "kiekprisnigo" means "where it is". I have no idea, I just used Google Translate, but that hints it isn't related to howmuchitsnowed.

To the OP, maybe it's a bit early, but....

One of the coding standards tells us to be clear about names. "howmuchitsnows" isn't "comfortable", it seems to me. Among the example solutions proposed (interpreted for this context), these might be better choices:

snow_quantity
howMuchItSnowed
how_much_it_snowed
snow_amount
amount_snowed
precipitation

These are different styles of shortened (slightly) versions of "howmuchitsnows". Creativity is encouraged, as is clarity, in choosing the name. Verbose, long names are discouraged, but sometimes what is too long is subjective. Separating words in camel case (howMuchItSnowed) is a common choice, but not universally accepted. Separating words with underscore (never leading) is often recommended by authoritative sources (including Stroustrup). Single word options, like precipitation, is generally preferred when they're applicable.




It is Lithuanian for “how much has [passed]”.

(Pashkët is Easter in Lithuanian.)
Last edited on
naming stuff is always a headache. I am 1-2 letter name kind of guy, and go back with a search and replace once its about done.
I find that if I do not have good names for things then I do not have a good grasp of what I am trying to do and/or of the algorithm.

I am a little amazed that we are picking on "kiekprisnigo", which seems easy enough to read to me, much like "filename" or "totalsnow" -- and it clearly indicates what it is.

I would be much more inclined to lecture about one or two letter variable names which obscure the code and make it hard to think about your algorithm.
yea the d / s/ m stuff is a bigger concern. Again, I DO that too, but I replace them before anyone else sees it.
I only use them when dealing with throwaways local to < 5 lines of code, and even then I tend to correlate names to use. n for an integer counter, or s for a string, re for a regular expression, etc.
Naming aside, the thinking in these:

OP: the "n" is how much more has to snow before a target has been reached. The n decreases as more snow appears.
1
2
3
4
5
while ( s >= n ){ // error in logic, 0 < n
  d++;
  s += m;
  n -= s;
}

Teacher: accumulate snow and compare that total to the fixed target.
1
2
3
4
5
6
int howmuchitsnowed = 0;
while ( howmuchitsnowed<n ) {
  kiekprisnigo += s; // error in name, howmuchitsnowed
  d++;
  s += m;
}

Both are valid.
Topic archived. No new replies allowed.