-
Did I do this correctly?
Ok...here is the problem:
"Write an interactive C++ program to prompt the user for a folder name, echoing the response to the display. Your program must store the folder name in a single variable. Show the results of running your program with the following inputs, as shown below,
[run]
Where is the input data file (example: c:\home\john\ )? c:\home\fred\
The data file is located in the folder: c:\home\fred\
[run]
Where is the input data file (example: c:\home\john\ )? c:\home\tom\
The data file is located in the folder: c:\home\tom\"
I worked on it, and I have a program that prints what he asked....
Code:
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
char f[50]; //this is the variable, which is a "char" type
cout << "Where is the input data file (example: c:\\home\\john\\)? \n";
cin >> f;
cout << "The data file is located in the folder: " << f << "\n";
system("PAUSE");
return 0;
}
Is this right? My only issue is this...it says "Your program must store the folder name in a single variable". Is "char" classified as a variable? If it is, then I'm done. Does he mean that I'm supposed to fit all of that text into an actual variable, as in "int"? My delightfully vague teach..
Thanks
-
Yes, char[] is a single variable. You can tell because that variable has a name. In this case, the name is f. It has the type char[] (not char, very different), and it is a single variable.
-
>>Does he mean that I'm supposed to fit all of that text into an actual variable, as in "int"?
What you're doing is fine. Assuming you're on a Win32 machine, if you really try (and do some cheap 'n tricky pointer stuff), you can manage to cram 4 characters into an int. And if you don't want to bother storing happy faces or whatever characters are in the 128-255 range, you can actually pack in 8 characters :) And if you run a good compression algorithm, you might be able to fit in even more. And if you use a double, you get 8 bytes instead of 4, so you can fit in at least double the information :D But that's rather advanced, and hardly good coding technique; more of a mind-twister type thing. I would suggest going with the char[] or std::string, since that's what you'll be doing in real life situations.