Thread: Did I do this correctly?

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    15

    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

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>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 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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Did I Install the Platform SDK Correctly?
    By bengreenwood in forum C++ Programming
    Replies: 7
    Last Post: 07-14-2008, 09:33 AM
  2. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  3. windows header files not linking correctly
    By skorman00 in forum Windows Programming
    Replies: 2
    Last Post: 04-13-2005, 11:14 AM
  4. inputFn with hash not assigning pointer correctly
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-22-2002, 11:17 PM
  5. Why does this not read in correctly?
    By bluebob in forum C Programming
    Replies: 9
    Last Post: 04-19-2002, 09:17 AM