EDIT:
accidentally put braces around each string
Okay, glad I could help. As for the
1 2
|
if (!(cin))
cin.clear();
|
That's just in case the "failbit" of cin is set. It happens when you try to use cin.operator>>() too many times in a row (at least, that's how it ends up getting set when I do it) a solution would be to use cin.get() or cin.getline() instead, but for simplicity I just use cin.operator>>(). The clear() function clears the failbit, so cin can be used again without accidentally being skipped (or, getting called but the user having no chance to input information).
Now, for your "prompt each time" problem, you can have a 2D array of chars hold each string, OR, you can have a 1D array of string objects. I'll do it with an array of strings...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include<iostream>
#include<cstring> //or #include<string.h> <- don't forget to add this part!
#include<cmath> //or #include<math.h>
using namespace std;
int main()
{
string Strings[6] =
{
"Type in value for a1: ",
"\nType in value for a2: ",
"\nType in value for b1: ",
"\nType in value for b2: ",
"\nType in value for c1: ",
"\nType in value for c2: "
};
string Morestrings[4] =
{
"Type in value for ax1: ",
"\nType in value for bx2: ",
"\nType in value for ay2: ",
"\nType in value for by2: "
};
double dis[3][2];
double car[2][2];
for (int i = 0, k = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
cout << Strings[k];
if (!(cin))
cin.clear();
cin >> dis[i][j]
k++;
}
}
for (int i = 0, k = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << Morestrings[k];
if (!(cin))
cin.clear();
cin >> car[i][j];
k++;
}
}
return 0;
}
|
That's the edited code. Don't forget about k, and don't forget to include the string header file!
I use k as an index to which string to output in the loop. k updates in each nested loop (so we get to the next string), but it needs to be created in the outer for loop so it doesn't get reset every time we finish a row. There's no conditional test for k, but if you look closely you'll see you won't need to test it, because the number of strings is equal to the number of rows in both outer for loops, meaning testing i and testing k, would be identical.
I'm a bit uncomfortable with
because I'm not sure if it will just output the first character of each string in the array, or if it'll output the entire string. Tell me how it goes! If strings aren't being displayed properly, I do have a fix for it, but again, it'll be a bit more cryptic. Hope this helps.