I am new to C++ and programming in general, and am working on an upcoming homework assignment. I have run into several problems and would greatly appreciate assistance. The assignment is to write a code to determine area and volume in C++, using a While Loop. I am stuck with the while statement, I am not sure how to state when I want it to stop. Below is what I have so far.

#include <iostream>
using namespace std;
 
int area (int l, int w); // function prototype
 
int volume (int l, int w, int d); // function prototype
 
int main (int argc, char* argv[]) {
 
      int length, width, depth; // parameters for the areas and volumes
 
      int result; //result is output
 
      cout << endl;
 
      // read in the values of length, width, and depth
 
While (?)
{
      cout << "If you want the result of the calculation to be an area,";
      cout << endl;
 
      cout << "place a 0 in the depth parameter ";
      cout << endl << endl;
 
      cout << "Enter a length (postive integer): ";
      cin >> length;
 
      cout << "Enter a width (postive integer): ";
      cin >> width;
 
      cout << "Enter a depth (postive integer): ";
      cin >> depth;
 
      cout << endl;
 
      if (depth == 0) {
            result = area (length, width);
            cout << "With a length of " << length;
            cout << " and a width of " << width;
            cout << " the area is " << result << endl;
      } else {
            result = volume (length, width, depth);
            cout << "With a length of " << length;
            cout << " a width of " << width;
            cout << " and a depth of " << depth;
            cout << " the volume is " << result << endl;
 
      } // end if
 
      } // end loop
      return 0;
 
} // end main function
 
int area (int l, int w) {
      
      int result;
      result=area(l,w);
      return result;
      
} // end area function
 
int volume (int l, int w, int d) {
 
int result;
result=volume(l,w,d);
return result;
 
} // end volume function

Thank you,
bmgee13

>I am stuck with the while statement, I am not sure how to state when I want it to stop.

Usually you tell the user to enter a negative number to quit when dealing with numbers (assuming of course, that you don't need negative numbers from the input). So the condition would be something like

while (input >= 0)

The only problem is that you usually ask the user to enter a negative number at the first input statement at the prompt. So then you must ask the user for the dimensions of the box before the program will exit.

You can always break out of the loop if you find that the number is negative, which is probably the easiest for you (for now).

>Isnt it meant to be like: While(car = red)

That would be wrong, because = is an assignment operator. You'd be assigning car to red , which would be rather pointless, and quite likely you'd enter an infinite loop (if red never changed).

Thank you for your assistance, I do appreciate it. Although I am still unclear on the while statement, I have now used
While(result !=0). However, I am now receiving the error message
error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup I tried to look up this error message in the MS Help section, but am not receiving as much information as I would have hoped for.

It's got something to do with Microsoft wanting t_main with their compiler, although I don't really know much about it.

I'd research it a little bit more, except I want to watch the hockey game. Sorry :P

Thank you, joeprogrammer. I added the 't' to _main and it seems to have fixed that problem. I will need to research it a bit more to fully understand it though. Enjoy the game!

Thank you for your assistance, I do appreciate it. Although I am still unclear on the while statement, I have now used
While(result !=0). However, I am now receiving the error message
error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup I tried to look up this error message in the MS Help section, but am not receiving as much information as I would have hoped for.

Its not a problem with the code but with your project configuration. The most common reason you get this errror is when you don't create a console project.

This article has been dead for over six months. Start a new discussion instead.