C++ homework, finding a letter from a file

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions C++ homework, finding a letter from a file
# 1  
Old 01-18-2012
C++ homework, finding a letter from a file

Write a program which asks user to enter a string and that string saves in a .txt file. After the file has been saved your program must count how many time letter 'a' has been reapeated ?

Use fstream, string, and cctype libraries to make your jobe easier.

I wrote following code
Code:
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <cstdlib>

using namespace std;

int main(void)
{
    string input, exit;
    int let_a = 0;;
    fstream text("test.txt", ios::in | ios::out | ios::app );

    cout << "Enter string: ";
    cin  >> input;

    if (!text)
    {
        cout << "Error at stream opening" << endl;
        system("PAUSE");
        exit(EXIT_FAILURE);
    }

    text << input;

    while ( !text.eof() )
    {
        while ( getline(text, exit) )
                {
                    for (int i = 0; i < exit.length(); i++)
                    {
                            if ( toupper(exit[i]) == 'A')
                            {
                                let_a++;
                            }
                    }
                }
        }
     text.close();

    cout << "total A: " << let_a << endl;

    system("PAUSE");
    return 0;
}

I googled and also asked my friends but nobody can find an error Smilie
# 2  
Old 01-18-2012
This User Gave Thanks to zaxxon For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific letter in a file by other letter

Good afternoon all, I want to ask how to change some letter in my file with other letter in spesific line eg. data.txt 1 1 1 0 0 0 0 for example i want to change the 4th line with character 1. How could I do it by SED or AWK. I have tried to run this code but actually did not... (3 Replies)
Discussion started by: weslyarfan
3 Replies

2. Shell Programming and Scripting

Multiplying lines of file that contain certain letter by value

I am trying to remove the last letter in a file and then multiply each line (which contained this letter) by 500. This is what I have: 1499998A 1222222A 1325804A 1254556 1235 9998 777 cat /tmp/listzz |gawk '{print $4}'|gawk '{gsub(//, ""); print } This removes the A... (1 Reply)
Discussion started by: newbie2010
1 Replies

3. Shell Programming and Scripting

Getting the non-homogenous letter row from a text file

I do have a large tab delimited file with the following format CCCCCGCCCCCCCCCCcCCCCCCCCCCCCCCCC 23 65 3 4 AAAAAAAAAAAAAAAAaAAAAAAAAAAAAAAAA 24 6 89 90 TGTTTTTTTTTTTTGGtTTTTTTTTTTTTTTTT 2 4 8 90 TTTT-TTTTTTTTTTTtTTTTTTTTTTTTTTTT 1 34 89 50 GGGGGGGGGGGGGGGGTGGGGGGGGGGGGGGGG 87 6 78 66... (8 Replies)
Discussion started by: Lucky Ali
8 Replies

4. Shell Programming and Scripting

check index of a string by finding a letter in it

i would like to search for a letter in a string and get its index position. example: name='john' pos=$(expr index $name o) the result will be equal to 2 (2nd position) how do you make this thing not case sensitive? example: name='john' pos=$(expr index $name O) the... (1 Reply)
Discussion started by: kokoro
1 Replies

5. Shell Programming and Scripting

Get all File names starting with letter P

Hi, I have lets say 10 files , I need to process them one by one. So I need a command to get one file name at a time to process it into a variable Example Files P1111.dat P3344.dat S344.dat ... v_file_name = 'p111.dat' .. I will rename it to something after processing ... (1 Reply)
Discussion started by: prassu
1 Replies

6. UNIX for Dummies Questions & Answers

Display File with a specific letter

Hi im a noob in Unix Do you guys know what command display you the files that have the character n in there name im not looking that they have the n in the beginning or in the end im looking that in search the entire string to see if it have the character n i try this ls n* but only show... (2 Replies)
Discussion started by: Kaziduz
2 Replies

7. Shell Programming and Scripting

Finding a letter in a string

How to check whether a particular string contains dot or not? Here I can not use grep as the string is not in a file. I will get this string from user input Thanks, (2 Replies)
Discussion started by: balamv
2 Replies

8. Shell Programming and Scripting

Shell - Matching 3 letter file names

Hi I am running a shell script with bdf command and want to match all files with length 3 inside a specific partitions. How do i do that say for example if i want to list all files with length 3 in /home/jimmy partition, bdf /home/jimmy the output i need is xyx abc yyy amp ... (4 Replies)
Discussion started by: PrasannaKS
4 Replies

9. UNIX for Dummies Questions & Answers

i need 100th occurance of a letter in file

Hi to all, I am looking a file in vi editor to get 100th occurance of a latter in that file. Can any one help me in this? Thanks Sathish (1 Reply)
Discussion started by: bsathishmca
1 Replies

10. Shell Programming and Scripting

how to find capital letter names in a file without finding words at start of sentence

Hi, I want to be able to list all the names in a file which begin with a capital letter, but I don't want it to list words that begin a new sentence. Is there any way round this? Thanks for your help. (1 Reply)
Discussion started by: kev269
1 Replies
Login or Register to Ask a Question