Question on use of classes

This is a discussion on Question on use of classes within the C++ Programming forums, part of the General Programming Boards category; Hello all. I have a very simple exercise to do. On notepad, I create a file called filelab.txt. The only ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    10

    Question on use of classes

    Hello all. I have a very simple exercise to do. On notepad, I create a file called filelab.txt. The only thing in this file is the sentence "C++ is fun" (without the quotes, just the sentence). So what I'm supposed to do is: Read in each letter(from the file) separately, and then display (on the screen) as it is read in from the file, stopping at "n".
    2 conditions:
    1) Do not use a loop to read.
    2) I must use classes.

    I have finished this problem, and it works fine, but without classes. I'm not really sure how to do it with classes.
    Here's the code:
    Code:
    #include <fstream.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    
    int main ()
    {
    	ifstream infile("A:filelab.txt", ios::nocreate); //this opens up the file
    	char a, b, c, d, e, f, g, h;
    	infile >> a;
            cout << a << endl;
    	infile >> b;
            cout << b << endl;
    	infile >> c;
            cout << c << endl;
    	infile >> d;
            cout << d << endl;
            infile >> e;
    	cout << e << endl;
    	infile >> f;
    	cout << f << endl;
    	infile >> g;
    	cout << g << endl;
    	infile >> h;
    	cout << h<< endl;
    	
    	infile.close();
    	return 0;
    
    }
    now my questions are:
    1) Where would the "ifstream infile("A:filelab.txt", ios::nocreate); //this opens up the file " command go, in the source file? I don't think it goes in the main or the header file, but not sure.
    2)What is the class of? All the program does is read a sentence from a file, and output to screen. So what is the class about in this case, the sentence? I'm having a hard time with this, what's the class, the object, in the problem?

    Thanks for any feedback!
    Last edited by Flyer; 06-23-2003 at 06:38 PM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    ifstream infile("A:filelab.txt", ios::nocreate);

    you are creating an instance of the CLASS ifstream

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    You mean the input stream itself is the class?
    And would
    ifstream infile("A:filelab.txt", ios::nocreate);
    go in the source file, the main file, or the header?
    Last edited by Flyer; 06-23-2003 at 07:16 PM.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Strange assignment!

    You are probably supposed to create a class. Maybe something like TextClass that has a member function to read the characters from the file, and has variables that hold and display the characters. (?)

    If you have time, maybe you should ask your instructor to clarify what the object is... or to ley\t you know if you're on the right track.

    Have you studied character arrays (or strings) yet? Because it would be better to have Letter[0] = 'C', Letter [1]= '+', etc.

    And of course, "Read each letter separately" really cry's-out for a loop... But, you gotta follow the instructions.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    10
    Yeah, actually I did send him an email, but still no answer and this is due tomorrow
    We have seen character arrays and strings, but it didn't occur to me to do it that way. Why do you think that way would be better?
    And thanks for answering!

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Well, now it's too late, but...

    With a character array, you don't have to add new variables if the text-string gets longer. And, character arrays are a standard (normal) way of handling strings.

    However, upon further thought... in your program, you only need one variable! Since you are displaying the character immediately, and you don't need to save it... You can overwrite the variable with a new value each time you read a new character.

    Code:
    infile >> a;
    cout << a << endl;
    infile >> a;
    cout << a << endl;
    infile >> a;
    cout << a << endl;

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    I really see no real advantage or practical use in making a class for this project. Its stupid asking you to make a class.

    Without a loop, you can do recursion:
    Code:
    void readchar(ifstream in)
    {
       char c;
    
       in >> c;
       cout << c;
    
       if (c != 'n')
          readchar(in);
    }
    And oh yeah, use the newer headers for C++. And in fact, you don't need all those libraries.
    Code:
    #include <fstream>
    //#include <cstdlib>     // don't need this
    //#include <cstdio>      // don't need this either
    
    using namespace std;

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    2

    Cool

    stupid project, looks grim.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Only thing I can see using a class for (and this is really stretching it) is to have an object which will read in a character at a given position, and then populating an array with these objects (by using something like SPeedy5's code). Why you would want a class to do that though, I'm not entirely sure.

    Note: In the recursive readchar function, it would be wise to check the stream state as well.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 02:46 AM
  2. Replies: 2
    Last Post: 07-28-2006, 03:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 03:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 08:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 11:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21