Thread: This code wont work

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1

    Angry This code wont work

    Someone please tell me what is wrong with this code:

    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    
    int main ()
    {
       using namespace std;
       
       char in_file_name[16], out_file_name[16], next;
       int ws, letters;
    
       ifstream fin;
       ofstream fout;
       
       cout << "I will find the total number of occurrences of characters in the file "
            << "total number of nonwhitespace characters, and the total number of letters "
            << "in a file\n";
       cout << "Enter the input file name (maximum of 15 characters) :\n";
       cin >> in_file_name;
       cout << "Enter the output file name (maximum of 15 characters) :\n";
       cin >> out_file_name;
    
       fin.open(in_file_name);
       if (fin.fail())
       {
          cout << "Input file opening failed.\n";
          exit(1);
       }
    
       fout.open(out_file_name);
       if (fout.fail())
       {
          cout << "Output file opening failed.\n";
          exit(1);
       }
    
        fin.get(next);
        while (! fin.eof())
        {
           if (isspace(next))
              ws ++;
           
           fin.get(next);
        }
    
        fin.get(next);
        while (! fin.eof())
        {
           if (isalpha(next))
              letters ++;
           
           fin.get(next);
        }   
    
       fin.close();
       fout.close();
    
       cout << "The total number of whitespaces was " << ws <<endl;
       cout << "The total number of letters was " << letters << endl;
       cout << "Thank you for using this program\n";
       
       return 0;
    }
    Code tags added by Kermi3

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    It isn't enclosed in code tags.

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    In the furture please use Code Tags. People will be much more likely to help you if you do. And they'll be happy about it

    Information on code tags may be found at the link on my signature.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    >Someone please tell me what is wrong with this code:

    Your compiler can tell you what is wrong with your code. Did it ? What do you want your code to do, what does it do instead ? Does it compile, does it link ? You are presenting us a lot of work we have to do to help you. You already did this work and don't share. Not funny. Give us information.


    >Someone please tell me what is wrong with this code:

    Without the information I'd say it doesn't let me switch into first person view and hunt down some jap planes.


    Edit:
    Just noticed that this was your very first post... sorry for being sarcastic, but please post more info, because you already have it and we could really use it to help you.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The code looks fine... except for 2 small bugs

    [list=1][*]Always initialize your variables
    Code:
    int ws = 0, letters = 0;
    [*]You count the number of whitespaces and when you're at the end of your input file you start counting the number of letters.

    What you need to do is check for whitespace AND letter in the same while loop:
    Code:
    while (! fin.eof())
    {
           if (isspace(next))
              ws ++;
    
           else if (isalpha(next))
              letters ++;
           
           fin.get(next);
    }
    [/list=1]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-15-2007, 01:36 AM
  2. Very short code tt never work...please help
    By newbie1234 in forum C Programming
    Replies: 7
    Last Post: 05-23-2006, 11:46 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM