#include <fstream>

#include <cctype>

#include <iostream>

#include <string>

const int num=1024;

using namespace std;
int main(){


ifstream file;
string s;
int i=0;

file.open("words1.txt");

while(getline(file,s)){

for(int i=0; i< s.length(); i++){

cout << s;//prints out the text file just fine. "a dog."

cout << s[0]; /*prints out random stuff. <---my problem. All I want is for this program to print out the char 'a' anywhere i put my s[0].*/
}


}

cout << endl;


file.close();

}

Hmm... seems to work fine for me (Mac OS X 10.6/g++)

Random stuff... you mean a lot of 'a' characters? When I ran your program as-is, here was the output (my input file has 2 lines - "a dog" and "a cat"):

aa adaoagaaa acaaata

Of course, that's because each trip through the loop, you emit the "s[0]". To break up the lines, add:

cout << endl;

after your "for" loop, and each line in your file will wind up on it's own line in the output.

Are you seeing other characters, other than 'a's? What platform are you developing on?

when I take the cout << s[0]; and move it out of my for loop it doesnt print anything at all. To my understanding s[0] should print out the first letter of my string. Do you know how I can fix this?

Oh and I have the same system as you. One other thing that I still can't get is for my program to read only one space and give an error if the scentence has two spaces this is what I got so far. If(s.length()!=' '){ cout << "ERROR"; nothing happens.;(

I put this line of code right below the "while" but before the "for" loop:

cout << endl << "First char of line: '" << s[0] << "'" << endl;

I get this:

First char of line: 'a'
a dog
First char of line: 'a'
a cat
First char of line: ''

(The last one is since I have a blank line in my file at the end).

About your other issue. The code:

if(s.length()!=' ')
   cout << "ERROR"

won't work, because "s.length()" returns an integer, and you're comparing it to a space (or two spaces, which is really a string, and not a character). You can inject some simple code in your "for" loop to check for spaces - but you have to count them. Declare an int called "iSpaceCount", initialize it to zero, and then put this in your "for" loop:

if (s[i] == ' ')
{
    iSpaceCount++;
}
else
{
    iSpaceCount = 0;
}
if (iSpaceCount > 1)
{
    cout << "Too many spaces!!!" << endl;
}

See if that gets you a bit further.

Hmm... seems to work fine for me (Mac OS X 10.6/g++)

Random stuff... you mean a lot of 'a' characters? When I ran your program as-is, here was the output (my input file has 2 lines - "a dog" and "a cat"):

Of course, that's because each trip through the loop, you emit the "s[0]". To break up the lines, add:

cout << endl;

after your "for" loop, and each line in your file will wind up on it's own line in the output.

Are you seeing other characters, other than 'a's? What platform are you developing on?

//What I meant was that all I want is the first character of the first line to be read even if it has a /t or a space. EX. All the kids /n like pie <--all I want to read is the A at the beginning.

I really appreciate your help. I have been on this for a whole day. Is there anything I can do to sharpen my understanding/skills on C++ other than coding through trial and error??

I'm not really sure why the code you have is not producing that. Further, perhaps if I knew what the program was trying to accomplish, I might be able to give more directed insight. So far, I've been trying to address the specifics of what you say is not working.

Is this for an assignment? Is this just an example but not part of your actual program?

Sometimes, trial and error is the best way. You muddle through things, gaining understanding of how the library functions work and how to construct the code to do what you want. What may not be fun is when you have to do all of that on a tight schedule.

Well this is not an assignment its practice problems. I resolved the capitalization part all I need is to fix is if my scentence has a period at the end of my text file and ill be done.. Here is all of my code.

#include <fstream>

#include <cctype>

#include <iostream>

#include <string>

const int num=1024;

using namespace std;
int main(){


  ifstream file;
  string s;
  int SpaceCount=0;
  int TabCount=0;
  int whitespace=0;
  int period=0;
file.open("words1.txt");

 while(getline(file,s)){
   if (s[0] == ' ' )    {

     whitespace++;

   }

   else

     {

       whitespace = 0;

     }


if(s[0] >='a' && s[0]<='z'){
     cout << "You are missing a capital in the beggining of your scentence."<<endl;
cout << endl << "First char of line: '" << s[0] << "'"  << endl;
}
   for(int i=0; i< s.length();i++){
     cout << s[i];

        if (s[i] == '.'){ //not working properly.
          period++;}
else{period =0;}
        if(period <0){
 cout << "You are missing a period at the end of your scentence." << endl;
        }




if (s[i] == ' ' )    {

     SpaceCount++;

     }

        else

           {

               SpaceCount = 0;

           }

   if (SpaceCount > 1)
 {

    cout << "Too many spaces." << endl;

    }
 }
   cout << endl;
/*     if (s[i] == '/t' )    {                                                                               
                                                                                                                
       TabCount++;                                                                                              
                                                                                                                
     }                                                                                                          
                                                                                                                
     else                                                                                                       
                                                                                                                
       {                                                                                                        
                                                                                                                
         TabCount = 0;                                                                                          
                                                                                                                
       }                                                                                                        
                                                                                                                
     if (TabCount > 1)                                                                                          
                                                                                                                
       {                                                                                                        
                                                                                                                
         cout << "Too many tabs." << endl;                                                                      
                                                                                                                
         }*/ not working properly.

 }
 file.close();


}

Take the period check out of your loop, and add this right after the "for" loop:

if (s[s.length() - 1] != '.')
{
    cout << "Missing period at end of line!" << endl;
}

haha I had that piece of code but I never had a cout statement I'm an idiot.lol
Just to make it nice how in hell do I get this program to ignore leading and trailing white space??

EX. [space]space]Hello Mike![space][space] <----how do I ignore the [space] and just read the text "Hello Mike!" I have been looking everywhere for advice/help and all they tell me is use isspace but I have no fn clue how also they tell me that my capitalization,period wont work with multiple lines because my program reads the trailing [space} and leading[space].
I know you have helped me a lot so if you don't want to answer this last question it's cool.=(


THANK YOU Mr.Mike!

Try this (put at the very top of the "while" loop):

string::iterator it = s.begin();
while (*it == ' ')
{
        s.erase(it);
}
string::iterator itb = s.end() - 1;
while (*itb == ' ')
{
        s.erase(itb);
        itb--;
}

Thank you man. You smart person you ciao. =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.