I'm having trouble with my programming assignment. It's made up of two parts. Part I is a while loop, and part II is a for loop.

The first part counts how many even numbers and how many odd numbers the user types before typing 0. I think I have most of this part complete, but I don't know how to make it determine the odds from the evens. I know I need to use

if (num%2 == 0)

, but I don't know where it goes, and what else I need to add.

In the second part, the user types in two temperatures. The first temperature is mulitplied by 5 and it loops until the temperature is greater than or equal to the second temperature. I'm pretty sure this is how I set up the rest of the code, but again I'm not sure where it goes.

cout << “   Celsius  Fahrenheit” << endl;
cout << “------------------------” << endl;
cout << setw(8) << celsius << setw(8) << fahrenheit << endl;

I have part ii set up how it's essentially supposed to be, but I'm having a hard time putting the pieces together.

And here's the code I have written so far:

#include<iostream>

using namespace std;



const double fahrenheit = 9.0 / 5 * celsius + 32;


int main()

{

   int count = 0, i, j;

   char number;

   

   cout << "Enter a non-zero integer (0 to quit): ";

   cin >> number;   

      

   while (number != '0')

   {

   	count = count + 1;

   	cout << "Enter a non-zero integer (0 to quit): ";

   	cin >> number; 

   }

   cout << "-----------------------------------------" << endl;

   cout << "Even Count:  " << count << endl;

   cout << "Odd Count:   " << count << endl;

   cout << "-----------------------------------------" << endl;



   int i, j;



   cout << "Enter beginning Celsius temperature: ";

   cin >> i;

   cout << "Enter ending Celsius temperature:    ";

   cin >> j;

 

   for(i = 0; i < 11; i++)

   {

      for(j = 1; j <= i; j++)

         cout << "*";

      cout << endl;

   }

   

   return 0;

}

Here's an example of the output:
Enter a non-zero integer (0 to quit): 89
Enter a non-zero integer (0 to quit): 55
Enter a non-zero integer (0 to quit): 56
Enter a non-zero integer (0 to quit): 1
Enter a non-zero integer (0 to quit): 2
Enter a non-zero integer (0 to quit): 0
-----------------------------------------
Even Count: 2
Odd Count: 3
-----------------------------------------
Enter beginning Celsius temperature: 25.891
Enter ending Celsius temperature: 58.111
Celsius Fahrenheit
------------------------
25.891 78.604
30.891 87.604
35.891 96.604
40.891 105.604
45.891 114.604
50.891 123.604
55.891 132.604

I hope this isn't confusing. I appreciate any help I may receive, and I want to let it be known that I hope I can return the favour to other users on this forum because I know how frustrating this is.

Thanks in advance!

Recommended Answers

All 6 Replies

use the mod % operator to check of the number is odd or even. When (number % 2) == 0 the number is even.

make the data type of number an int, not a char because you can't enter a number greater than one digit in a char. You will also have to change the while loop on line 26 while( number != 0)

I know that I need to add "if (num%2 == 0)", but I don't know where it goes. I don't understand why I need to change "while( number != 0)" because I was pretty sure that's how it's suppose to be. When the number is not equal to 0, it loops. In other words, the program will keep going until I type in zero. It's suppose to do that.

As for part two. Knowing what to put in the for loop would help me tremendously. I know that my program has to multiply the temperature by 5 for each loop, but I'm not sure how to set this up.

Thanks.

In your while loop you will need to include a counter for both odd and even numbers. Also inclyde the if else statement in the while loop. For example:

while(number != 0)
{
//ask for and accept the number from the user
if(number%2==0)
//increment even counter
else
//increment odd counter

}

The only thing you need to change in the while loop is to make 0 an integer not a character with quotes. This is what Ancient Dragon was trying to point out to you.

In your while loop you will need to include a counter for both odd and even numbers. Also inclyde the if else statement in the while loop. For example:

while(number != 0)
{
//ask for and accept the number from the user
if(number%2==0)
//increment even counter
else
//increment odd counter

}

The only thing you need to change in the while loop is to make 0 an integer not a character with quotes. This is what Ancient Dragon was trying to point out to you.

My apologies to Ancient Dragon. It was a stupid mistake on my part. In the lab, we used a character to end the loop, and I didn't even notice.

I think I have the part I set up the way it's suppose to be, but the terminal keeps telling me there's a parse error before else (line 27). Here's what I have now:

#include<iostream>

using namespace std;

int main()

{

   int number, even = 0, odd = 0;

   cout << "Enter even integer: ";

   cin >> number;

   while(number != 0)

   {

   cout << "Enter even integer: ";

   cin >> number;

   if(number%2==0)

      even++

   else

      odd++

   }

   cout << "-----------------------------------------" << endl;

   cout << "Even Count:  " << even << endl;

   cout << "Odd Count:   " << odd << endl;

   cout << "-----------------------------------------" << endl;

   return 0;

}

Thanks.

You need to put semicolons after the statements in the if/else statements. (i.e. you need semicolons after the increment statements.)

You need to put semicolons after the statements in the if/else statements. (i.e. you need semicolons after the increment statements.)

Thanks, I appreciate it. It wasn't working correctly at first because I had odd++ and even++ mixed up, but I figured it out, lol. I'm still having some trouble with part ii though. I'm not sure how to set it up, but I don't think I have much more work to do on it. I got the for loop from an example in my book, and I'm not sure how to connect it with fahrenheit and celsius or if I have set the for loop up correctly. Thanks.

#include<iostream>
using namespace std;
const double

int main()
{
  int celsius, fahrenheit;

  cout << "Enter beginning Celsius temperature: ";
  cin >> i;
  cout << "Enter ending Celsius temperature:    ";
  cin >> j;

  fahrenheit = 9.0 / 5 * celsius + 32;

  for(i = 0; i < 11; i++)

  {
     for(j = 1; j <= i; j++)
        cout << "*";
     cout << endl;
  }

  cout << “   Celsius  Fahrenheit” << endl
  cout << “------------------------” << endl;
  cout << setw(8) << celsius << setw(8) << farenheit << endl;

  return 0;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.