Sponsored Content
Homework and Emergencies Homework & Coursework Questions C++ homework, finding a letter from a file Post 302591139 by solaris_user on Wednesday 18th of January 2012 03:47:25 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
FBB::TempStream(3bobcat)					 Temporary fstream					  FBB::TempStream(3bobcat)

NAME
FBB::TempStream - Temporary fstream SYNOPSIS
#include <bobcat/tempstream> DESCRIPTION
FBB::TempStream objects are temporary std::fstreams opened with mode 0600 (user only read+write). The file created by a TempStream object is removed from the file system once the TempStream object goes out of scope. A TempStream object creates an empty file, and information can immediately be inserted into the TempStream object. To switch between inser- tion and extraction simply call seekg (for extraction) or seekp (for insertion). NAMESPACE
FBB All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB. INHERITS FROM
std::fstream CONSTRUCTORS
o TempStream(std::string const &base = "/tmp/FBB::TempStream"): The constructor initializes the object and creates a file with the given base-name to which six random characters are appended. If the TempStream could not be constructed an FBB::Errno exception is thrown. The copy constructor is not available. MEMBER FUNCTIONS
All std::fstream members are available, as FBB::TempStream inherits from this class. In addition, TempStream itself offers o std::string const &fileName() const: The name of the created temporary file. EXAMPLE
#include <iostream> #include <bobcat/tempstream> using namespace std; using namespace FBB; int main() { TempStream ts("/tmp/demo"); ts << "Hello world "; ts.seekg(0); string line; getline(ts, line); cout << line << ", removed: " << ts.fileName() << ' '; } FILES
bobcat/tempstream - defines the class interface SEE ALSO
bobcat(7), mkostemp(3) BUGS
None reported. DISTRIBUTION FILES
o bobcat_3.01.00-x.dsc: detached signature; o bobcat_3.01.00-x.tar.gz: source archive; o bobcat_3.01.00-x_i386.changes: change log; o libbobcat1_3.01.00-x_*.deb: debian package holding the libraries; o libbobcat1-dev_3.01.00-x_*.deb: debian package holding the libraries, headers and manual pages; o http://sourceforge.net/projects/bobcat: public archive location; BOBCAT
Bobcat is an acronym of `Brokken's Own Base Classes And Templates'. COPYRIGHT
This is free software, distributed under the terms of the GNU General Public License (GPL). AUTHOR
Frank B. Brokken (f.b.brokken@rug.nl). libbobcat1-dev_3.01.00-x.tar.gz 2005-2012 FBB::TempStream(3bobcat)
All times are GMT -4. The time now is 06:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy