13 Replies - 998 Views - Last Post: 09 February 2011 - 12:40 PM Rate Topic: -----

#1 Guest_Brad*


Reputation:

Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 08:32 AM

I am doing a Temperature Function Table and I can not get my function to Loop. It seems like it is looping 1 time instead of looping until it reaches the end. (For example, you start at 10, and end at 20. You increment at 5. It is only doing the calculation once, when it starts at 10 and that's it)

My code below, as you can see I am doing a Menu, but it is mostly the FOR loop at the end is hat I am stuck on and I have tried count++, as you see throughout the FOR loop trying to get it loop but can't.

#include <iostream>
#include <cctype>

using namespace std;

int displayMenu();
char getMenuSelection(char &choice);
int getStartEndAndIncrement(int &start, int &end, int &increment);
int cToF(int start, int end, int increment, int &count);

int main ()
{
    char choice;
    int start = 0;
    int end = 0;
    int increment = 0;
    int count = 0;
  
   while(1)
   {  
        displayMenu();
        getMenuSelection(choice);
        getStartEndAndIncrement(start, end, increment);
        cToF( start, end, increment, count);
        
   }    
     cin.ignore(2);   
     return(0);       
}
int displayMenu()
{
    cout << "\t\t\tTemperature Table Functions\n" << endl;
    cout << "1. Press letter 'C' to go from Celcius to Fahrenheit " << endl;
    cout << "2. Press letter 'F' to go from Fahrenheit to Celsius " << endl;
    cout << "3. Press letter 'Q' to quit the program\n " << endl;               
}
char getMenuSelection(char &choice)
{
    cout << "Which choices would you like to try: " << endl;
    cin >> choice; 
    choice = toupper(choice);
    while(1)
    {
        if (choice == 'C' || choice == 'c' || choice == 'F' || choice == 'f')
        {
            return choice;           
        }
        else if (choice == 'Q')
        {
            exit(1);     
        }
        else
        {
            cout << "Invalid Choice. Must choose between: C, F, or Q: " << endl;
            cin >> choice;    
        }
     }
}
int getStartEndAndIncrement(int &start, int &end, int &increment)
{
    cout << "What is the temperature you would like to start with: " << endl;
    cin >> start;
    cout << "What is the temperature you would like to end with: " << endl;
    cin >> end;
    cout << "What would you like to Increment the temeperature by: " << endl;
    cin >> increment;                         
}
int cToF(int start, int end, int increment, int &count)
{
    double fahrenheit;
    
        for (count = start; count <= end; count = count + increment)
        {
             cout << count << " ";
             for (count = start; count <= end; count = count + increment)
             {  
                 fahrenheit = (5.0 * (start - 32.0))/9.0;
             }
             cout << fahrenheit;    
        } 
        count++; 
}




Is This A Good Question/Topic? 0

Replies To: Need Help With My FOR Loop - Not Looping

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 08:40 AM

int cToF(int start, int end, int increment, int &count)
{
        
        double fahrenheit;
    
        for (count = start; count <= end; count = count + increment)
        {
             cout << count << " ";
             for (count = start; count <= end; count = count + increment)
             {  
                 fahrenheit = (5.0 * (start - 32.0))/9.0;
             }
             cout << fahrenheit;    
        } 
        count++; 
}


You are using count for the outer for loop but also modifying it inner for loop. That is why the for loops don't work properly.

Use another variable for inner loop.

This post has been edited by chinchang: 09 February 2011 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Brad*


Reputation:

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 09:31 AM

View Postchinchang, on 09 February 2011 - 08:40 AM, said:

int cToF(int start, int end, int increment, int &count)
{
        
        double fahrenheit;
    
        for (count = start; count <= end; count = count + increment)
        {
             cout << count << " ";
             for (count = start; count <= end; count = count + increment)
             {  
                 fahrenheit = (5.0 * (start - 32.0))/9.0;
             }
             cout << fahrenheit;    
        } 
        count++; 
}


You are using count for the outer for loop but also modifying it inner for loop. That is why the for loops don't work properly.

Use another variable for inner loop.


Chinch, that helped with the count, but I am having problems with the Fahrenheit calculation for the other increment. This is what I get:

Enter Starting Point - 10
Enter Ending Point - 20
Increment - 5

10_6.9251e-308
15_-12.2222
20_-12.2222

(underscore are spaces)

This is what I have now:

int cToF(int start, int end, int increment, int &count, int &tally)
{
    double fahrenheit;
    
        for (count = start; count <= end; count = count + increment)
        {
             cout << count << " " << fahrenheit;
             for (tally = start; tally <= end; tally = tally + increment)
             {  
                 fahrenheit = (5.0 * (start - 32.0))/9.0;
                 cout << endl;
             }    
        }  
}



Was This Post Helpful? 0

#4 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 10:52 AM

If I am right are you trying to convert all temperatures within a range(start to end) from C to F ?
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Brad*


Reputation:

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 11:04 AM

View Postchinchang, on 09 February 2011 - 10:52 AM, said:

If I am right are you trying to convert all temperatures within a range(start to end) from C to F ?


Thats correct, depending on the increment.
10 degree C --> degree Farenheit
15 degree C --> degree Farenheit
20 degree C --> degree Farenheit
Was This Post Helpful? 0

#6 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 11:16 AM

You don't need 2 for loops for that. Just keep one loop.


int cToF(int start, int end, int increment, int &count)
{
    double fahrenheit;
    
        for (count = start; count <= end; count = count + increment)
        {
             fahrenheit = (5.0 * (start - 32.0))/9.0;
             cout << endl;
                 
        }  
}


Also why do you pass the count variable from main(). There is no need to do so. Just declare it within the function itself.

int cToF(int start, int end, int increment)
{
    double fahrenheit;
    
    for (int count = start; count <= end; count = count + increment)
    {
         fahrenheit = (5.0 * (start - 32.0))/9.0;
         cout << endl;
                 
    }  
}

This post has been edited by chinchang: 09 February 2011 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#7 Guest_Brad*


Reputation:

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 11:35 AM

View Postchinchang, on 09 February 2011 - 11:16 AM, said:

You don't need 2 for loops for that. Just keep one loop.


int cToF(int start, int end, int increment, int &count)
{
    double fahrenheit;
    
        for (count = start; count <= end; count = count + increment)
        {
             fahrenheit = (5.0 * (start - 32.0))/9.0;
             cout << endl;
                 
        }  
}


Also why do you pass the count variable from main(). There is no need to do so. Just declare it within the function itself.

int cToF(int start, int end, int increment)
{
    double fahrenheit;
    
    for (int count = start; count <= end; count = count + increment)
    {
         fahrenheit = (5.0 * (start - 32.0))/9.0;
         cout << endl;
                 
    }  
}


IfI don't pass count in main(), I get this error:

24. invalid initialization of non-const reference of type 'int&' from a temporary of type '<unknown type>'

otherwise passing in main removes this error. So I am assuming I have to have count = 0 in the main().

Also, going towards the 1 FOR loop is giving me the wrong output. I am getting this when
start = 10, end = 20, increment = 5

10 _ 6.9251e-308
15 _ -12.2222
20 _ -12.2222

(Underscore is space)

As you can see the output is wrong.
Was This Post Helpful? 0

#8 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 11:44 AM

Change Line 24 to :

cToF( start, end, increment);


because now your function takes only 3 parameters. You don't need count in main()

Your function should be :

int cToF(int start, int end, int increment)
{
    double fahrenheit;
    
    for (int count = start; count <= end; count = count + increment)
    {
         fahrenheit = (5.0 * (count - 32.0))/9.0; // you had start instead of count
         cout << endl;
                 
    }  
}


By the way, as per your formula your function should be called fTOc, not cToF :)

This post has been edited by chinchang: 09 February 2011 - 11:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 Guest_Bad*


Reputation:

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 11:57 AM

View Postchinchang, on 09 February 2011 - 11:44 AM, said:

Change Line 24 to :

cToF( start, end, increment);


because now your function takes only 3 parameters. You don't need count in main()

Your function should be :

int cToF(int start, int end, int increment)
{
    double fahrenheit;
    
    for (int count = start; count <= end; count = count + increment)
    {
         fahrenheit = (5.0 * (count - 32.0))/9.0; // you had start instead of count
         cout << endl;
                 
    }  
}


By the way, as per your formula your function should be called fTOc, not cToF :)


Got that fix. I am still getting the wrong output.
I get this
10 _ 6.95251e-308
15 _ 50
20 _ 50

Now I know 50 is correct, but that should be for 10 degrees, not 15 and 20.
Was This Post Helpful? 0

#10 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 12:02 PM

Can you post your latest code.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Brad*


Reputation:

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 12:12 PM

View Postchinchang, on 09 February 2011 - 12:02 PM, said:

Can you post your latest code.


Heres my code:

 
#include <iostream>
#include <cctype>

using namespace std;

int displayMenu();
char getMenuSelection(char &choice);
int getStartEndAndIncrement(int &start, int &end, int &increment);
int cToF(int start, int end, int increment);

int main ()
{
    char choice;
    int start = 0;
    int end = 0;
    int increment = 0;
  
   while(1)
   {  
        displayMenu();
        getMenuSelection(choice);
        getStartEndAndIncrement(start, end, increment);
        cToF( start, end, increment);
        
   }    
     cin.ignore(2);   
     return(0);       
}
int displayMenu()
{
    cout << "\t\t\tTemperature Table Functions\n" << endl;
    cout << "1. Press letter 'C' to go from Celcius to Fahrenheit " << endl;
    cout << "2. Press letter 'F' to go from Fahrenheit to Celsius " << endl;
    cout << "3. Press letter 'Q' to quit the program\n " << endl;               
}
char getMenuSelection(char &choice)
{
    cout << "Which choices would you like to try: " << endl;
    cin >> choice; 
    choice = toupper(choice);
    while(1)
    {
        if (choice == 'C' || choice == 'c' || choice == 'F' || choice == 'f')
        {
            return choice;           
        }
        else if (choice == 'Q')
        {
            exit(1);     
        }
        else
        {
            cout << "Invalid Choice. Must choose between: C, F, or Q: " << endl;
            cin >> choice;    
        }
     }
}
int getStartEndAndIncrement(int &start, int &end, int &increment)
{
    cout << "What is the temperature you would like to start with: " << endl;
    cin >> start;
    cout << "What is the temperature you would like to end with: " << endl;
    cin >> end;
    cout << "What would you like to Increment the temeperature by: " << endl;
    cin >> increment;                         
}
int cToF(int start, int end, int increment)
{
    double fahrenheit;
    
        for (int count = start; count <= end; count = count + increment)
        {
             cout << count << " " << fahrenheit;
             fahrenheit = ((9.0/5.0) * start) + 32.0; 
             cout << endl;    
        }
}


Was This Post Helpful? 0

#12 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 12:20 PM

Please check my previous posts. You still have not corrected the errors.

What O/P do you get now?
Was This Post Helpful? 0
  • +
  • -

#13 Guest_Brad*


Reputation:

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 12:35 PM

View Postchinchang, on 09 February 2011 - 12:20 PM, said:

Please check my previous posts. You still have not corrected the errors.

What O/P do you get now?

This:
int cToF(int start, int end, int increment)
{
    double fahrenheit;
    
        for (int count = start; count <= end; count = count + increment)
        {
             cout << count << " " << fahrenheit;
             fahrenheit = ((9.0/5.0) * count) + 32.0; 
             cout << endl;    
        }
}


The O/P gives this:
10 _ 6.9251e-308
15 _ 50
20 _ 59

still wrong output.
Was This Post Helpful? 0

#14 Guest_Brad*


Reputation:

Re: Need Help With My FOR Loop - Not Looping

Posted 09 February 2011 - 12:40 PM

View PostBrad, on 09 February 2011 - 12:35 PM, said:

View Postchinchang, on 09 February 2011 - 12:20 PM, said:

Please check my previous posts. You still have not corrected the errors.

What O/P do you get now?

This:
int cToF(int start, int end, int increment)
{
    double fahrenheit;
    
        for (int count = start; count <= end; count = count + increment)
        {
             cout << count << " " << fahrenheit;
             fahrenheit = ((9.0/5.0) * count) + 32.0; 
             cout << endl;    
        }
}


The O/P gives this:
10 _ 6.9251e-308
15 _ 50
20 _ 59

still wrong output.


BINGO!! :)
Thank You Chinchang. It works now :)
Was This Post Helpful? 0

Page 1 of 1