Thread: Help on toupper string

  1. #1
    Registered User programmer's Avatar
    Join Date
    Oct 2001
    Posts
    22

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

  2. #2
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133
    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.
    C++ Is Powerful
    I use C++
    There fore I am Powerful

  3. #3
    Registered User programmer's Avatar
    Join Date
    Oct 2001
    Posts
    22
    Ok i got that space thing done, now what about the ouput ... its capitalizing everyword. forexample: Hello. My Name Is Joe.
    Plz help.
    Thanxs.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    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;
    }

  5. #5
    Registered User programmer's Avatar
    Join Date
    Oct 2001
    Posts
    22

    Question

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

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if((name[j] >= 'a') && (name[j] <= 'z'))
    &nbsp; 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM