PDA

View Full Version : C++ Help II



loldongs
13th March 2006, 08:09 PM
Yo, back for some more help.

Here's my problem

PROBLEM: A student of statics must find the angle in degree measure between any number of pairs of
vectors. The vector information will be available in one of three forms:
1. The rectangular components are known.

2. The magnitude and the directional angles theta x,y, and z of each vector are known.

3. The magnitude and the directional cosines lamda x, y, and z of each vector are known.

A menu must prompt the user for the form of the vector data. The program will then ask the user for the data as well as an identifying letter for each vector. The angle between the vectors will then be calculated. The original vector data and the resulting angle will be displayed to the screen and sent to an output file in an appropriate format.

Here's what i'm mainly worrying about...

4. Fully validate all user input. Be sure to check the logical state of cin.fail() after any input. Your program should cope if the user inadvertently enters a character instead of a number.
5. You may use the keyboard to input data but the output is to be displayed to the screen as well as stored to a file. Use a single output file to store the original vector data along with the results from all test cases.

I also have to use minimum of 5 functions, which isn't too bad, but I'm really sketchy on some of the finer points of functions, like using variables. I'll post what I have so far for my actual code later, but before that can anyone help with the testing for invalid input and the output file?

Seraphim
14th March 2006, 10:43 AM
Outing to text files, eh? Hmm....let's see if I still remember how to do this.

You're going to need the fstream library for this one, so up at your include statements stick #include <fstream> That'll give you access to three classes in your project: ofstream, ifstream, and fstream. If you're just outputing to a file, then all you'll need to use is ofstream. I'll leave a little commented code example below to help explain how to use it.



#include <fstream>

int main(){
ofstream fileToWrite; // comment 1
fileToWrite.open("sample.txt"); // comment 2
fileToWrite << "This is a line of text. \n"; // comment 3
fileToWrite.close(); // comment 4
return 0;
}
*comment 1 : declares an ofstream object called fileToWrite
*comment 2 : opens a connection with a textfile at the specified path. If no file by that name exists, the program will create one with that name.
*comment 3 : outputs the string to the file, works pretty much like the cout function as far as what you can do.
*comment 4 : don't forget to close the file when you're done with it.

Now, when you open up sample.txt it will read "This is a line of text"

As for your other question regarding the cin.fail(); Treat that as you would any boolean statement as it is simply a function that checks to see if the last input given by the user was valid. If the input was successful, it will return false, but if there was a problem it will return true. For the intents of your program I think it would be best to use it in a do while loop.



///EXAMPLE///
#include <iostream>
int main(){
int num;
do{
cout<<"Enter a number ";
cin>>num;
}while(!cin.fail())
return 0;
}

What this code does is prompt the user until a valid input has been received and when it does, it stores that value in num.

Hope that helps.

loldongs
19th March 2006, 05:42 PM
Alright it all looks good to me. So do you put your boolean statement inside the con.fail(boolean condition here?) kind of thing?

If so, how could I test for character value when it is supposed to be numerical?


Fully validate all user input. Be sure to check the logical state of cin.fail() after any input. Your program should cope if the user inadvertently enters a character instead of a number.

After that I should be pretty good.

Seraphim
19th March 2006, 06:49 PM
the .fail() method has no parameters. You don't have to put any statements inside the parentheses. cin.fail() itself is the boolean statement so you'd put it inside anywhere you that needs a boolean like an if statement or a while loop.


If so, how could I test for character value when it is supposed to be numerical?

It's been a while since I've used C++ but I believe the fail() method will check all that automatically. From what I've read from some quick searches, whenever you use cin to store input to a variable the computer checks whether the input operation was completed successfully and sets the value "true" or "false" to some location we can't see. Then, when the fail() method is called, it simply retrieves that information.

I don't know a heck of a lot about how strict C++ is with data type checking, but I think a cin can fail if you pass data that doesn't agree with the type you are trying to input to. For example lets say you have the statement cin>>num; where num is declared as an int. If the user tries to pass data that can't be parsed into an int (i.e. a string that doesn't consist entirely of numbers, or a decimal) the cin will fail to complete and cin.fail(); will return "true" the next time we ask it, so long as we don't have anymore cin statements in between.

The only roadblock I can see is if C++ considers things of data type char as parseable to int since letters all have an integer ASCII value. I wish I knew how to put all this in simpler terms, but use a do-while loop for each input similar to the model I provided you in the first post. If you're still having problems in testing, post your code and I'll try to clarify anything I might've not explained well.

Xull
19th March 2006, 09:14 PM
Alright it all looks good to me. So do you put your boolean statement inside the con.fail(boolean condition here?) kind of thing?Was already said that fail has no parameters, but anyway... more details :P

Basically, you input your value (such as an integer) then check if it succeeded or not.

If it was unable to parse the expected datatype (such as it found letters or other unexpected characters when it expects a number), then the fail state is set.
Next, you can check cin.fail() and if there was an error, handle it appropriately, such as calling cin.clear() (clears the failure) and using cin.ignore (skipping characters to something valid) or asking for correct input.


If so, how could I test for character value when it is supposed to be numerical?well, if you still need it (whatever method you are going to use to get the correct input), you could consider using cin.peek() :)

loldongs
20th March 2006, 06:01 PM
aight, I know what you are saying, but when I try to input it into my code it doesn't do what I want. Where did I screw up?

here's a function that's is supposed to assign a letter value to a vector. Problem is, 'char' also accepts low integer values as well as single character entries, so this doesn't work. What should I use instead? Or should I just have a very long boolean condition (in a while loop or something) that stops the program if the user is a douchebag?



char getVecLetter(char* note)
{
cout<<"Enter the letter that you would like to assign to the "<<note<<" vector\n";

char letter;
cin>>letter;

while (cin.fail())
{
cin.clear();
cout<<"please re-enter the letter value for the vector\n";
cin>>letter;
}
return letter;
}



Also there's this one (another function), where I need only numerical entry, nothing else. But when I run the program and try to see if it works it does the loop infinitly. This is what I have;



double getVecInfo(char* note1)
{
cout<<"Please enter the "<<note1<<" component for the vector\n";

double value;
cin>>value;

while (cin.fail())
{
cout<<"Your input was invalid. Please re-enter the value\n";
cin.clear();
cin>>value;
}
return value;
}

Seraphim
20th March 2006, 06:52 PM
char getVecLetter(char* note)
{
cout<<"Enter the letter that you would like to assign to the "<<note<<" vector\n";

char letter;
cin>>letter;

while (cin.fail())
{
cin.clear();
cout<<"please re-enter the letter value for the vector\n";
cin>>letter;
}
return letter;
}



I think you can compensate for that if you know that the ASCII values for capital letters are 65-90 inclusive and the values for lower case are 97-122 inclusive. Since it seems that low ints and chars seem to be interchangeable simply modifying your while condition might fix it, like so.



while(cin.fail() && ((char>=65 && char<=90)||(char>=97 && char<=122)))
{
cin.clear();
cout<<"please re-enter the letter value for the vector\n";
cin>>letter;
}

There may be a solution might be a little simpler than that but the second long condition ensures that even if the user is a douchebag and decides to enter an integer that the integer value they entered corresponds to a letter.

As for your second question. I'm not seeing anywhere in that method that should produce an infinite loop, so are you sure it's repeating something inside that method or could it be that some outer loop is calling that function infinitely?

loldongs
20th March 2006, 07:38 PM
The
re may be a solution might be a little simpler than that but the second long condition ensures that even if the user is a douchebag and decides to enter an integer that the integer value they entered corresponds to a letter.

Sweet, I changed what you had slightly, but still used the ASCII method. I forgot I had those in my text. That part of my code is now douchbag proof :P


As for your second question. I'm not seeing anywhere in that method that should produce an infinite loop, so are you sure it's repeating something inside that method or could it be that some outer loop is calling that function infinitely?
Yeah, it's pretty much the exact same (in terms of order of statements) as the character function, but when i get there and try it, it says "reenter etc" infinitly down my screen.