loading

What is my problem with this program?

I am using C++

I know it has something to do with the 'cin' command.  I am trying to get numbers for an array, and I've heard that 'cin' doesn't work for arrays.  The array is supposed to be variable in length.

#include <iostream>
#include <cmath>
using namespace std;

int main () {
    string input = "";
    short int choice = 0;
   
    cout << endl << "This weight and balance calculator will determine the cg of your aircraft." << endl;
   
    if (true) {
          cout << "Press any key to continue: " << endl;
          getline (cin, input);
          if (choice != 0.0000001);
          {
                    goto WB;
                    }
                   
WB:
         
   int w = 0;
          float d = 0;
          float s = 0;
   int m = w;
   float tw = 0;
   float tm = 0;
   int weight[w];
   int moment[m];
         
          while (true) {
          cout << endl << "Please enter the amount of masses within the aircraft, including fuel and the aircraft itself: ";
   cin >> w;
   cout << endl << "Please enter the weight of the aircraft, occupants, fuel, baggage, etc.: ";
          cin >> weight);
          cout << endl << "Please enter the moment of each listed item (Weight x Arm = Moment): ";
          cin >> moment);
   cout << endl << "Enter the total weight: ";
   cin >> tw;
   cout << endl << "Enter the total moment: ";
   cin >> tm;
   float cg = tm / tw;

          cout << endl << "The cg is: " << cg << endl;
          }
}
}

sort by: active | newest | oldest
kelseymh5 years ago
Um. Did you actually paste in source code which has compiled successfully? You have extraneous semicolons, a mismatched (and unnecessary) if statement, extraneous parentheses, and you're apparently allocating C-style arrays of zero length (which is forbidden).

As written, your code won't compile successfully, let alone do anything useful.

Since you're using C++, why don't you just declare a pair of vectors? Then do a loop until the user says they have nothing more to enter (e.g., a negative weight), read in a single weight and moment on each iteration and push the two values back onto the vectors?

And why do you require the user to enter the total weight and moment at the end? Shouldn't those just be the sum of the components?
KaydenST (author)  kelseymh5 years ago
As of now, it doesn't compile successfully. The arrays are meant to be variable in length. I am trying to give the 'cin >> w;' and 'cin >> m;' to assign the length of the arrays. What are vectors? I guess I wasn't thinking real well about the totals, I guess I could do that. Thanks for your comment.

P.S. I forgot to tell you, the reason that it won't compile is because it doesn't recognize the 'operator>>' in the cin statements here for some reason. Again, I've heard that the cin operator doesn't work well with arrays. Here is the code:

int w = 0;
float d = 0;
float s = 0;
int m = w;
float tw = 0;
float tm = 0;
int weight[w];
int moment[m];

while (true) {
cout << endl << "Please enter the amount of masses within the aircraft, including fuel and the aircraft itself: ";
cin >> w;
cout << endl << "Please enter the weight of the aircraft, occupants, fuel, baggage, etc.: ";
cin >> weight);
cout << endl << "Please enter the moment of each listed item (Weight x Arm = Moment): ";
cin >> moment);
cout << endl << "Enter the total weight: ";
cin >> tw;
cout << endl << "Enter the total moment: ";
cin >> tm;
float cg = tm / tw;

cout << endl << "The cg is: " << cg << endl;
}
}
}
This still won't compile. You have "weight);" which is meaningless. That's what I meant by the extraneous parentheses. You also have extra closing braces which don't match any opening braces. You have also set up an infinite loop (while (true)), bad programming practice.

You don't read a whole array with one input statement. As I wrote above, you create a loop. Then you read a single array element each time inside the loop. When the loop is finished, your array will be full.
KaydenST (author)  kelseymh5 years ago
For some reason, DevC++ would come up with an error about the braces. It said that I needed another closing brace. I didn't notice the parenthesis after weight "weight)", and I will fix that. The infinite loop is so that the person doesn't have to open the program every single time they want to calculate the cg, they could just use the same window. It also lets them see their previous calculations. Can you please show me the loop to use? I am pretty new to C++.
Would this be the right loop?

while (i = 0; i < 10; i++)
{
code
}
Yes. But replace "10" by "w", or whatever variable you use to store the number of entries to be made.

I would also suggest getting yourself a basic C/C++ textbook.
KaydenST (author)  kelseymh5 years ago
I've got a book (Beginning Programming for Dummies), but that is an all around book. I am thinking about getting a sole C++ book though... Thank you soooooo much for your help! I will add the loop for right now. Is there anyway to cin an array, just out of curiosity?
Yes, you could use an istream iterator. However, you really need a better understanding of C++ flow control before you start using complex STL structures. Work through getting a simple loop to work first, then deal with the more complex issue.