-
Help on toupper string
Hi ... First of all here is my code:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int j;
fstream datafile;
char name[81];
datafile.open("demofile.txt",ios::in);
if(!datafile)
{
cout << "File open error!"<<endl;
return 0;
}
while(!datafile.eof())
{
datafile >> name;
for (j = 0; j < 1; j++){
if((name[j] >= 'a') && (name[j] <= 'z')){
*name = toupper(*name);
}
}
cout << name;
}
datafile.close();
cout << "\nDone.\n";
return 0;
}
This code runs perfectly fine , i hve a small problem ... the string which i'm suppose to get as an output is: "Hello. My name is Joe. What is your name ?"
And i'm gettin' :"Hello.MyNameIsJoe.WhatIsYourName?"
Why is it doin' this ??
Please help ..
Thanxs.
:confused:
-
Well you are not checking for spaces.
Code:
for (j = 0; j < 1; j++){
if((name[j] >= 'a') && (name[j] <= 'z')){
*name = toupper(*name);
}
}
You also have to check for " " space here.
-
Ok i got that space thing done, now what about the ouput ... its capitalizing everyword. forexample: Hello. My Name Is Joe.
Plz help.
Thanxs.
-
Code:
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s);
typedef string::iterator SI;
for (SI i = s.begin(); i != s.end(); ++i)
*i = toupper(*i);
cout << s << endl;
return 0;
}
-
Thanxs Nick , but thats not what i want ...
i hve problem with my output ... so if u just scroll up n hve a look at the problem, u will get the idea.
Thanxs anywayz ..
-
> if((name[j] >= 'a') && (name[j] <= 'z'))
if ( islower(name[j]) )
would be better
> datafile >> name;
This reads a word, not a line.
Use the getline function as suggested by Nick.
You need to consider whether this is the first word on the line, or the first word after a full stop.