Character array

This is a discussion on Character array within the C++ Programming forums, part of the General Programming Boards category; Hello everyone! I hope someone can help me. I have a program to make using functions that will read in ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    Character array

    Hello everyone!

    I hope someone can help me. I have a program to make using functions that will read in a list of single words from a sequential file so that it can calculate the word value based upon formulas for individual seleted letters. I am to manipulate each word in different ways which I think I can handle, but I don't know how to read in the words one by one in order to calculate and print the value of each words.

    The problem is that I only get 1 out of every 2 letters. And if the word has an odd number of letter, I get garbage at the end. I think I may be going about it the wrong way. Can anybody help me?

    Thank you in advance, it is much appreciated.


    #include "a:bcc.h"
    #define cls system ("cls")

    //************************************************** *********************** type definitions

    ofstream outfile; // Global definition of saving devices
    ifstream infile;

    //************************************************** *********************** function prototypes

    void read(char warray[]);
    //void print(char warray[], int &i);


    //************************************************** *********************** main function


    void main(void)
    {
    char word_array[21];
    int count=0;
    cls;
    infile.open ("wordcalc.txt");

    read(word_array);
    // print(word_array, count);


    } // END MAIN FUNCTION

    //************************************************** *********************** function definitions

    void read (char warray[])
    {

    int count=0;


    while(infile.get() != '\n')

    {
    infile >> warray[count];
    cout << warray[count];
    count++;
    }


    cout << count;
    infile.close();


    } // END READ FUNCTION


    //************************************************** ***********************
    #include "a:bcc.h"
    #define cls system ("cls")

    //************************************************** *********************** type definitions

    ofstream outfile; // Global definition of saving devices
    ifstream infile;

    //************************************************** *********************** function prototypes

    void read(char warray[]);
    //void print(char warray[], int &i);


    //************************************************** *********************** main function


    void main(void)
    {
    char word_array[21];
    int count=0;
    cls;
    infile.open ("wordcalc.txt");

    read(word_array);
    // print(word_array, count);


    } // END MAIN FUNCTION

    //************************************************** *********************** function definitions

    void read (char warray[])
    {

    int count=0;


    while(infile.get() != '\n')

    {
    infile >> warray[count];
    cout << warray[count];
    count++;
    }


    cout << count;
    infile.close();


    } // END READ FUNCTION


    //************************************************** ***********************
    #include "a:bcc.h"
    #define cls system ("cls")

    //************************************************** *********************** type definitions

    ofstream outfile; // Global definition of saving devices
    ifstream infile;

    //************************************************** *********************** function prototypes

    void read(char warray[]);
    //void print(char warray[], int &i);


    //************************************************** *********************** main function


    void main(void)
    {
    char word_array[21];
    int count=0;
    cls;
    infile.open ("wordcalc.txt");

    read(word_array);
    // print(word_array, count);


    } // END MAIN FUNCTION

    //************************************************** *********************** function definitions

    void read (char warray[])
    {

    int count=0;


    while(infile.get() != '\n')

    {
    infile >> warray[count];
    cout << warray[count];
    count++;
    }


    cout << count;
    infile.close();


    } // END READ FUNCTION


    //************************************************** ***********************
    #include "a:bcc.h"
    #define cls system ("cls")

    //************************************************** *********************** type definitions

    ofstream outfile; // Global definition of saving devices
    ifstream infile;

    //************************************************** *********************** function prototypes

    void read(char warray[]);
    //void print(char warray[], int &i);


    //************************************************** *********************** main function


    void main(void)
    {
    char word_array[21];
    int count=0;
    cls;
    infile.open ("wordcalc.txt");

    read(word_array);
    // print(word_array, count);


    } // END MAIN FUNCTION

    //************************************************** *********************** function definitions

    void read (char warray[])
    {

    int count=0;


    while(infile.get() != '\n')

    {
    infile >> warray[count];
    cout << warray[count];
    count++;
    }


    cout << count;
    infile.close();


    } // END READ FUNCTION


    //************************************************** ***********************
    Attached Files Attached Files
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    #include <iostream.h>
    #include <fstream.h>

    void main()
    {
    const int MAX_WORD=10;
    char Word[MAX_WORD];

    ifstream InFile;

    InFile.open("Test.txt");

    InFile >> Word;

    while(!InFile.eof())
    {
    cout << Word; //Display last word
    InFile >> Word;
    }

    getchar();
    }

    haven't compiled this, but this should read a word at time. As long as each word is on a new line within the file your reading in.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    Unhappy

    Thanks for the post, but unfortunately I had thought of that. All it gives me at run time is a blank screen... I am really at a loss....

    I need those letters to go in one by one into an array, convert them to upper case, then calculate a value for each individucal letter, add those values up for every single word, display a count of the total words in the file (which is unknown) the average word lenght and the average of all the word values. This needs to be done using functions and parameter passing.
    I have no problems using functions, but I am quite the moron when it comes to parameter passing.


    I am just adding this not because I want people to write the program, but because it may help in understanding what I need to do with the letters that enter the array.
    Oh and by the way I am truly sorry about the multiple post of my code. I really don't know how that happened.
    Any help would be appreciated. I have been trying for 2 days and am still in front of this computer trying to figure out how to do it.
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    variables local to main():
    ifstream inFile -----to read in words from file
    char input[30] -----to hold each word as it is read in
    int wordCount = 0; --to keep track of the number of words read
    int word Value; ----to give numeric value to letters in word
    int wordLength ---length of current word
    long totalValue ---running total of all word value for all words read
    long totalLength---running total of length of all words read
    double avgValue---running average value of letters in avg word
    double avgLength--running average length of average word

    function declarations:
    int CalculateWordValue(char *);
    void CalculateAvgValue(int, long, double &);
    void CalculateAvgLen(int, long, double &);

    Code:
    function definitions:
    int CalculateWordValue(char * Input)
    {
      //local variables
      int temp = 0;
      int length = strlen(Input);
      int i;
      char ch;
    
      //calculations done locally
      for(i = 0; i < length; i++)
      {
         //convert each char in Input to upper case
         ch = toupper(Input[i]);
         temp += //conversion value for ch;
       }
        return temp;
    }
    
    void CalculateAvgValue(int WordCount, long totalValue, double & avgValue)
    {
      avgValue = totalValue/WordCount;
    }
    
    //simialr for CalculateAvgLength()
    
    Program psuedocode:
    #includes:  
    fstream.h for I/O and file handling routines
    whatever file toupper() is in
    
    //function declarations listed here
    
    int main()
    {
      declare variables local to main();
      //open ifstream and associate with file to be read
      //use loop to read in each word in file
      while(fin >> input)
      {
         wordCount++;
    
         wordValue = CalculateWordValue(input);
          TotalValue += wordValue;
          CalculateAvgValue(wordCount, totalValue, avgValue);
    
          //determine wordLength;
          //determine totalLength;
          CalculateAvgLength(wordCount, totalLength, avgLength);
       }
        
        //display results after everything read in
        word Count;
         average value of average word;
         average length of average word;
      
         return 0;
    }
    
    //function definitions listed here
    Last edited by elad; 04-01-2002 at 02:36 PM.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Thank you both for your replies. It was very much appreciated. I got finally got it to work with a lot of pain and effort. Some of what was said here certainly helped. Thanks again!!!
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Array comparison
    By magda3227 in forum C Programming
    Replies: 7
    Last Post: 07-09-2008, 09:36 AM
  2. Replies: 7
    Last Post: 05-11-2008, 11:57 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. two dimensional character array
    By feuerraeder in forum C Programming
    Replies: 4
    Last Post: 11-22-2002, 08:59 AM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM

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