-  [JOIN IRC!]


[Return]
Posting mode: Reply
Name
Subject   (reply to 696)
Message
File
Password  (for post and file deletion)
¯\(°_O)/¯
  • Supported file types are: BMP, GIF, JPG, PNG
  • Maximum file size allowed is 10000 KB.
  • Images greater than 400x400 pixels will be thumbnailed.
  • Currently 317 unique user posts. View catalog

  • Blotter updated: 2015-09-02 Show/Hide Show All


File 141632280882.jpg - (62.83KB , 500x500 , 0.jpg )
696 No. 696
I've decided to try my hand at learning C++, it's my first programming language I've actually put some serious effort into learning. Problem is quite early on in the book I'm reading (C++ Primer, 5th ed.) I am stumped on one of the exercises.

I'm supposed to write a program that asks the user for two numbers, then prints off the numbers in descending order. I can't get it to work properly though, and I feel like I have to add another variable in but I don't know what I'd do.

The issues, specifically, are that for one, it won't output any numbers if the first number is larger than the second. Secondly, it won't print the whole range if I do enter the numbers in the correct order. This is what happens:


michael@linux-1ic7:~/projects/CPP_Primer/build> ./cpp_primer
25
1
michael@linux-1ic7:~/projects/CPP_Primer/build> ./cpp_primer
1
25
25
24
23
22
21
20
19
18
17
16
15
14
13


This is the code I have written so far:

#include <iostream>
using namespace std;
int main()
{
int x=0, y=0;
cin >> x >> y;
for (int val=x; val <= y; val++)
cout << y-- << endl;
return 0;
}


If someone could help me figure this out it'd be greatly appreciated.
>> No. 724
Lets trace through what you do.

int x=0, y=0;
cin >> x >> y;

You get two inputs and store them in ints. x is the first one, y is the second.

for (int val=x; val <= y; val++)

You loop from x up to y. What will happen if x is bigger than y? `val <= y` will be false and the loop won't run.

cout << y-- << endl;

You decrement `y` and print it out. You're changing `y` and `val` together, so that `val <= y` becomes false twice as quickly as you expect.

You probably want to do something like this:

for (int val=y; val >= x; val--)
cout << val << endl;

Then just swap `x` and `y` if you get them in the wrong order.
>> No. 749
Kind of late, but I may be helping somebody with this.

As far as I can tell, you're only required to take two numbers and print those two numbers. Your program is not only changing the values of the two numbers, but is using a loop to do so repeatedly, which is not only unnecessary but problematic, even dangerous if working with important data.

I'm not going to point out the problems with the loop because a loop is entirely unnecessary. The task can be done with just the two integers and a control statement.

int main()
{
int x=0, y=0;
/*
cout << "Enter two numbers: ";
Giving prompts to the user is a good idea if you want other people to know how to use your program.
*/
cin >> x >> y;

if(x > y)
cout << x << '\n' << y << endl;
else
cout << y << '\n' << x << endl;
}

The program either prints x then y, or y then x. You DO NOT change the numbers for a simple printing job. If you wanted to swap the values of x and y for the sake of one cout statement, then you would need to change x and y, but to do that without a third dummy variable is a test I'll leave for you.


Delete post []
Password  
Report post
Reason