LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-23-2016, 12:05 PM   #1
iceman81
Member
 
Registered: Jan 2013
Location: New Jersey
Distribution: CentOS, Arch
Posts: 181

Rep: Reputation: Disabled
C++ while problem.


hey guys, i am working my way through C++ primer 5th ed. and in the first chapter it gives a problem to solve.

problem: write a program that prompts the user for two integers. Print each number in the range specified by those two integers.

I was really dumbfounded at first, as this was different from the examples before. So i searched online for an example to go through, and they used the "Print_Range" function.

Now at this early in the book, there is no mention of this function, so I conclude that that can't be what they are looking for.

Here is my code that i came up with, which I don't think is 'correct', but it gets the job done.

Also- I am using while loop, because that was the last topic before this question.

Code:
#include <iostream>
int main()
{
  int v1 = 0, v2 = 0;  //setting 2 variables for cin
  std::cout << "Please enter 2 integers: " << std::endl;
  std::cin >> v1 >> v2; 
  std::cout << "The numbers between " << v1 << " and " << v2 << " are: "
	    << std::endl;
  int v3 = v2 - 2; // while loop was going over v2, added v3 to compensate.
  while(v1 <= v3) {
    ++v1;
    std::cout << v1 << std::endl;
  }

  return 0;
}

Last edited by iceman81; 07-23-2016 at 12:09 PM.
 
Old 07-23-2016, 12:43 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You have to determine first which variable (v1 or v2) has the least value, then count from it until the other value
Assigning least and greatest values to start and end variables could be useful

I would use a for loop instead as it tests variable value and increments, while while loop requires another line (like in your code)
 
1 members found this post helpful.
Old 07-23-2016, 12:52 PM   #3
iceman81
Member
 
Registered: Jan 2013
Location: New Jersey
Distribution: CentOS, Arch
Posts: 181

Original Poster
Rep: Reputation: Disabled
thanks for responding, the section on 'for' is directly after, and it does make much more sense. Maybe moving forward i'll wait to finish the chapter to try the exercises =P. thanks again.
 
Old 07-23-2016, 01:30 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Just another thought, you can also write the increment in the while loop
Code:
while(++v1 <= v3) {
    std::cout << v1 << std::endl;
}
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
iptables - problem with rules( VPN tunnel and problem with access to server in LAN) jargrzyb Linux - Networking 2 09-05-2017 05:28 AM
UEFI problem, GRUB2 problem, blank screen problem, :( JackDinn Linux - Newbie 22 05-26-2015 01:57 PM
Do I have a path problem, an Apache2 problem or a Javascript problem or any other pro rblampain Linux - Networking 0 12-29-2010 03:50 AM
Sound Card problem(every time i install linux i have diffirent hardware problem) jacka1l Linux - Newbie 7 08-11-2005 06:10 AM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration