# loop help

Page 1 of 1

## 13 Replies - 403 Views - Last Post: 04 March 2018 - 11:42 AMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=409637&amp;s=4abea47fd37e370387d47f20d92d7e50&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lm34

Reputation: 0
• Posts: 7
• Joined: 02-March 18

# loop help

Posted 02 March 2018 - 06:44 PM

hi, just starting out and learning. I am wanting this program to ask the user within the loop to input the first(start) and last(end) values inside the table.

here is what I have:
```// this program tables celsius to fahrenheit temperatures

#include <iostream>
#include <iomanip>

using namespace std;

int main()

{
double celsius, fahrenheit, celsius20;

cout << "this tables shows the temperatures of Celsius to Fahrenheit in the ranges of 0 to 20 and the equivalents\n";

cout << "Enter 0 for celsius to start: \n";
cin >> celsius;
while (celsius != 0)
{
cout << "invalid entry "
<< "enter 0 for celsius to start: ";
cin >> celsius;
}

fahrenheit = (celsius * 9) / 5 + 32;
cout << " Celsius       Fahrenheit\n"
<< "----------------------\n";

cout << setprecision(2) << fixed;
cout << right << setw(6) << celsius;
cout << setw(14) << fahrenheit << endl;

for (celsius = 1; celsius <= 19; celsius++)
{

fahrenheit = (celsius * 9) / 5 + 32;

cout << setprecision(2) << fixed;

cout << right << setw(6) << celsius;
cout << setw(14) << fahrenheit << endl;

}
cout << "----------------------\n";
cout << "Enter 20 for celsius to end program: \n";
cin >> celsius;
cout << "----------------------\n";
while (celsius != 20)
{
cout << "invalid entry "
<< "enter 20 for celsius to end: ";
cin >> celsius;
}
fahrenheit = (celsius * 9) / 5 + 32;

cout << setprecision(2) << fixed;
cout << right << setw(6) << celsius;
cout << setw(14) << fahrenheit << endl;

system("pause");
return 0;
}

```

This post has been edited by Skydiver: 02 March 2018 - 07:54 PM
Reason for edit:: Put code in code tags. Learn to do this yourself.

Is This A Good Question/Topic? 0

## Replies To: loop help

### #2 Skydiver

• Code herder

Reputation: 6062
• Posts: 20,872
• Joined: 05-May 12

## Re: loop help

Posted 02 March 2018 - 07:57 PM

If what you want is for the user to enter the range within a loop, why are you forcing the user to enter 0, and then to enter 20, and you never loop back to ask the user to enter another range?

### #3 baavgai

• Dreaming Coder

Reputation: 7111
• Posts: 14,826
• Joined: 16-October 07

## Re: loop help

Posted 03 March 2018 - 02:54 AM

Hmm... time to learn functions. For instance, you type fahrenheit = (celsius * 9) / 5 + 32; over and over. Instead:
```double toFahrenheit(double celsius) { return (celsius * 9) / 5 + 32; }

```

Consider how you might make a function for all that messy output formatting, for example.

Now, for a loop, you can again use a function:
```bool done = false;
while(!done) {
done = talkToUser();
}

```

Hope this helps.

### #4 Slyde

Reputation: 1
• Posts: 36
• Joined: 11-February 18

## Re: loop help

Posted 03 March 2018 - 10:09 AM

lm34:

I'm just starting to learn C++ too. You'll get a lot good help here in DIC. But if you have the time, check out this tutorial. It's well crafted, thoughtfully explained and up-to-date. And the author involves you in every chapter, a lot of hands on. I hope you find it beneficial.

This post has been edited by Slyde: 03 March 2018 - 10:11 AM

### #5 lm34

Reputation: 0
• Posts: 7
• Joined: 02-March 18

## Re: loop help

Posted 03 March 2018 - 03:47 PM

Skydiver, on 02 March 2018 - 07:57 PM, said:

If what you want is for the user to enter the range within a loop, why are you forcing the user to enter 0, and then to enter 20, and you never loop back to ask the user to enter another range?

I just want the user to input the first and last value of the table loop such as 0 and 20 ( I changed the ranged to 1-19 and forced it outside). I did so because I do not know how to ask the user to input those two values within the loop.

### #6 lm34

Reputation: 0
• Posts: 7
• Joined: 02-March 18

## Re: loop help

Posted 03 March 2018 - 03:52 PM

baavgai, on 03 March 2018 - 02:54 AM, said:

Hmm... time to learn functions. For instance, you type fahrenheit = (celsius * 9) / 5 + 32; over and over. Instead:
```double toFahrenheit(double celsius) { return (celsius * 9) / 5 + 32; }

```

Consider how you might make a function for all that messy output formatting, for example.

Now, for a loop, you can again use a function:
```bool done = false;
while(!done) {
done = talkToUser();
}

```

Hope this helps.

I put the formula in there 3x because I had two cin statements and the value of celsius changed each time and the third was within the loop. I tried to copy and past your function and visual studios did not like it, even after playing around with it for a bit.

### #7 tony jay

Reputation: 18
• Posts: 50
• Joined: 31-January 18

## Re: loop help

Posted 03 March 2018 - 04:05 PM

You have already stated, in the program via a cout statement, what the bounds of your loop needs to be, yes...
Why on earth would one want to badger a user to enter values already known to the programmer?

This program does not need any user-input!

I understand that you just beginning your journey into programming, but, even at this early stage, you need to take a step back and plan what you are doing prior to tapping the keyboard - programming is MUCH more about thinking than typing...
Right now, what you are doing is precisely the opposite!

### #8 baavgai

• Dreaming Coder

Reputation: 7111
• Posts: 14,826
• Joined: 16-October 07

## Re: loop help

Posted 03 March 2018 - 04:22 PM

lm34, on 03 March 2018 - 05:52 PM, said:

I tried to copy and past your function and visual studios did not like it, even after playing around with it for a bit.

I find this unlikly. However, if you need to see it in action:
```#include <iostream>
#include <iomanip>

using namespace std;

double toFahrenheit(double celsius) { return (celsius * 9) / 5 + 32; }

int main() {
double celsius; // never used , celsius20;

cout << "this tables shows the temperatures of Celsius to Fahrenheit in the ranges of 0 to 20 and the equivalents\n";

cout << "Enter 0 for celsius to start: \n";
cin >> celsius;
while (celsius != 0) {
cout << "invalid entry "
<< "enter 0 for celsius to start: ";
cin >> celsius;
}

cout << " Celsius       Fahrenheit\n"
<< "----------------------\n";

cout << setprecision(2) << fixed
<< right << setw(6) << celsius
<< setw(14) << toFahrenheit(celsius)
<< endl;

for (celsius = 1; celsius <= 19; celsius++) {
cout << setprecision(2) << fixed
<< right << setw(6) << celsius
<< setw(14) << toFahrenheit(celsius)
<< endl;
}
cout << "----------------------\n";
cout << "Enter 20 for celsius to end program: \n";
cin >> celsius;
cout << "----------------------\n";
while (celsius != 20) {
cout << "invalid entry "
<< "enter 20 for celsius to end: ";
cin >> celsius;
}
cout << setprecision(2) << fixed
<< right << setw(6) << celsius
<< setw(14) << toFahrenheit(celsius)
<< endl;

system("pause");
return 0;
}

```

Step 2, as previously suggested:
```#include <iostream>
#include <iomanip>

using namespace std;

double toFahrenheit(double);
void showLine(double);

int main() {
double celsius; // never used , celsius20;

cout << "this tables shows the temperatures of Celsius to Fahrenheit in the ranges of 0 to 20 and the equivalents\n";

cout << "Enter 0 for celsius to start: \n";
cin >> celsius;
while (celsius != 0) {
cout << "invalid entry "
<< "enter 0 for celsius to start: ";
cin >> celsius;
}

cout << " Celsius       Fahrenheit\n"
<< "----------------------\n";
showLine(celsius);
for (celsius = 1; celsius <= 19; celsius++) {
showLine(celsius);
}
cout << "----------------------\n";
cout << "Enter 20 for celsius to end program: \n";
cin >> celsius;
cout << "----------------------\n";
while (celsius != 20) {
cout << "invalid entry "
<< "enter 20 for celsius to end: ";
cin >> celsius;
}
showLine(celsius);

system("pause");
return 0;
}

```

Indeed
```#include <iostream>
#include <iomanip>

using namespace std;

double toFahrenheit(double);
void showLine(double);
void botherUntil(double &, double target, const std::string &prompt);

int main() {
double celsius; // never used , celsius20;

cout << "this tables shows the temperatures of Celsius to Fahrenheit in the ranges of 0 to 20 and the equivalents\n";

botherUntil(celsius, 0, "start");
cout << " Celsius       Fahrenheit\n"
<< "----------------------\n";
showLine(celsius);
for (celsius = 1; celsius <= 19; celsius++) {
showLine(celsius);
}
cout << "----------------------\n";
botherUntil(celsius, 20, "end");
showLine(celsius);

system("pause");
return 0;
}

```

However, as I don't know why you hate your user enough to bother them like that, you could simply show the data:
```#include <iostream>
#include <iomanip>

using namespace std;

double toFahrenheit(double);
void showLine(double);

int main() {
const int begin = 0, end = 20;
cout << "this tables shows the temperatures of Celsius to Fahrenheit in"
<< " the ranges of " << begin << " to " << end << " and the equivalents" << endl
<< " Celsius       Fahrenheit" << endl
<< "----------------------" << endl;
for(int i=begin; i<=end; i++) { showLine(i);  }
cout << "----------------------" << endl;
system("pause");
return 0;
}

```

Hope this helps.

### #9 lm34

Reputation: 0
• Posts: 7
• Joined: 02-March 18

## Re: loop help

Posted 03 March 2018 - 04:39 PM

tony jay, on 03 March 2018 - 04:05 PM, said:

You have already stated, in the program via a cout statement, what the bounds of your loop needs to be, yes...
Why on earth would one want to badger a user to enter values already known to the programmer?

This program does not need any user-input!

I understand that you just beginning your journey into programming, but, even at this early stage, you need to take a step back and plan what you are doing prior to tapping the keyboard - programming is MUCH more about thinking than typing...
Right now, what you are doing is precisely the opposite!

I agree, if it were up to me I would leave it at the for loop with the range 0 to 20. However, the task is asking for a user input of 0 and 20 within the table. Although, clearly not the most efficient way, with the lack of know how. what I did was that aside from the 20 input as it falls outside the table due to asking for 20 input.

### #10 lm34

Reputation: 0
• Posts: 7
• Joined: 02-March 18

## Re: loop help

Posted 03 March 2018 - 04:53 PM

[quote name='baavgai' date='03 March 2018 - 04:22 PM' timestamp='1520119366' post='2356772']

lm34, on 03 March 2018 - 05:52 PM, said:

I tried to copy and past your function and visual studios did not like it, even after playing around with it for a bit.

I find this unlikly. However, if you need to see it in action:

Just looked at #1. I was using it incorrectly after the int main() . I didn't now you could do that before that. Thank you. that makes a lot more sense now. I'm looking at the rest now.

### #11 Skydiver

• Code herder

Reputation: 6062
• Posts: 20,872
• Joined: 05-May 12

## Re: loop help

Posted 03 March 2018 - 05:19 PM

This code asks for 5 pairs of strings and numbers in a loop:
```#include <string>
#include <iostream>

using namespace std;

int main()
{
for(int i = 0; i < 5; i++)
{
string word;
cout << "Enter a word: ";
cin >> word;

int number;
cout << "Enter a number: ";
cin >> number;

cout << "You entered " << number << " and " << word << "." << endl;
}

return 0;
}

```

You could do something similar and ask the user for the lower and upper limits of the range of values. I suspect that the 0 to 20 is the acceptable values for the lower and upper limits of the range. I don't think that your instructor is forcing the user to enter 0 as the lower limit, and 20 as the upper limit.

### #12 lm34

Reputation: 0
• Posts: 7
• Joined: 02-March 18

## Re: loop help

Posted 03 March 2018 - 05:54 PM

baavgai, on 03 March 2018 - 04:22 PM, said:

lm34, on 03 March 2018 - 05:52 PM, said:

I tried to copy and past your function and visual studios did not like it, even after playing around with it for a bit.

thank you for taking the time to post that. Yes, I am a very visual person.
on #1: I was using it incorrectly, I put it after int main() as I did not know you could do that before that. thank you that helps a bunch!!!!

I really like how you used botherUntil, I got a good laugh.

On #2-4 i get error codes: for #2 i get:

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "void __cdecl botherUntil(double &,double,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected]@YAXAANNABV?\$[email protected]?\$[email protected]@[email protected]@V?\$[email protected]@[email protected]@[email protected]@@Z) referenced in function _main

Severity Code Description Project File Line Suppression State
Error LNK1120 2 unresolved externals

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "void __cdecl showLine(double)" ([email protected]@[email protected]) referenced in function _main

it doesn't like the void, showline, or botherUntil. pretty from what i can tell it is saying it is declared, but undefined and among other things, not entirely sure.

### #13 lm34

Reputation: 0
• Posts: 7
• Joined: 02-March 18

## Re: loop help

Posted 03 March 2018 - 06:17 PM

Skydiver, on 03 March 2018 - 05:19 PM, said:

This code asks for 5 pairs of strings and numbers in a loop:

You could do something similar and ask the user for the lower and upper limits of the range of values. I suspect that the 0 to 20 is the acceptable values for the lower and upper limits of the range. I don't think that your instructor is forcing the user to enter 0 as the lower limit, and 20 as the upper limit.

Thank you, I will play around with this.
this is what he has in that regard: Create a loop to display a table of Celsius temperatures 0-20, and their Fahrenheit equivalents.Have the user input the start and end temperatures for the table.The table should show the values right-justified in columns, with two digits after the decimal.

I already submitted the assignment, however in previous assignments there has been no feedback for corrections and I know I did not do this correctly. I just want to learn I do not care so much about the grade. I really appreciate everyone's feedback here.

### #14 baavgai

• Dreaming Coder

Reputation: 7111
• Posts: 14,826
• Joined: 16-October 07

## Re: loop help

Posted 04 March 2018 - 11:42 AM

lm34, on 03 March 2018 - 07:54 PM, said:

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "void __cdecl showLine(double)" ([email protected]@[email protected]) referenced in function _main

Did you write the implementation code? Given the error, the answer is no.

To be clear, this will compile, link, and run fine:
```#include <iostream>
#include <iomanip>

double toFahrenheit(double celsius) {
return (celsius * 9) / 5 + 32;
}

void showCelsius(double celsius) {
std::cout << std::setprecision(2) << std::fixed
<< celsius << "C is "
<< toFahrenheit(celsius) << "F."
<< std::endl;
}

int main() {
showCelsius(4);
return 0;
}

```

This will compile, but will not link. It will give a similar error to what you're getting above:
```#include <iostream>
#include <iomanip>

// forward function declartion
// function body should follow
double toFahrenheit(double celsius);
void showCelsius(double celsius);

int main() {
showCelsius(4);
return 0;
}

```

This is because the compiler is looking for the implementation for those functions. It could be later in the program in another file entirely.

This is the above with the implementation:
```#include <iostream>
#include <iomanip>

double toFahrenheit(double celsius);
void showCelsius(double celsius);

int main() {
showCelsius(4);
return 0;
}

double toFahrenheit(double celsius) {
return (celsius * 9) / 5 + 32;
}

void showCelsius(double celsius) {
std::cout << std::setprecision(2) << std::fixed
<< celsius << "C is "
<< toFahrenheit(celsius) << "F."
<< std::endl;
}

```