I am trying to get a program working, and im half way there, I think. What the program must do is take input from the keyboard and then capitalize every first letter of every word. Here is my entire program so far...

#include <stdio.h>
#include <iostream>
using namespace std;

int main (int argc, char *argv[], char **env)
{
  char c, lastc = ' ' ;
  c = cin.get();
  do {
    cout.put(c);
    c = cin.get();
  if (lastc)
    {
    c = toupper(c);
    }
  } while ( !cin.eof());
  return EXIT_SUCCESS;
}

The part where I am trying to do this is at:

if (lastc)
    {
    c = toupper(c);
    }

Basically the approach I am taking is having the program to recognize when there is a space or ' ' and have it capitalize the first letter after that.

When executed now my program capitalizes the whole word. I have searched for a couple hours now on these forums and over the net and havent found any code that might help me out.

Is there a command in C++ that I could use to capitalize only the first letter and not the whole word?

>>Is there a command in C++ that I could use to capitalize only the first letter and not the whole word?

No. You have to do that yourself. The toupper() function works on only one character.

Instead of all those gets(), you would just get input from the keyboard in one huge string at one time, then parse that string and turn the first letter of each word into capital letter

std::string line;
getline(cin, line); // get the entire line
// now parse the string and capitalize each word

I do not know of any standard function that would perform this task. You could always fix your own code though. Do you understand what you wrote?

int main (int argc, char *argv[], char **env)
{
  char c, lastc = ' ' ;
  c = cin.get();
  do {
    cout.put(c);
    c = cin.get();
  if (lastc)
    {
    c = toupper(c);
    }
  } while ( !cin.eof());
  return EXIT_SUCCESS;
}

Your algorithm:
1. Get one character, put in 'c'
2. Output 'c' (no check for capitalization? hmm...)
3. Get one character, put in 'c'
4. If 'lastc' (it will always be a space, which is 32, which will evaluate to true. every single time...)
5. Go back to step 2 until there are no more characters to read

Yeah I understand what I have written so far but I don't understand how to break up each word and capitalize only the first letter.

Is there a way to check to see if there is a ' ', in other words to see if what I have declared as "lastc" is happening to capitalize the next character?

Basically, for each character, check if it is the first character in the line or if it has a space before it. If it does, capitalize it. And AncientDragon is right, it would make more sense to read the entire line in at once and process it within a variable.

Basically, for each character, check if it is the first character in the line or if it has a space before it. If it does, capitalize it. And AncientDragon is right, it would make more sense to read the entire line in at once and process it within a variable.

So in my Do/while loop I would need to enter some type of code to check to see if the char is capitalized? Is there anywhere I could go to get some information on this?

I have only been programming for a couple weeks and my knowledge base of commands is quite small.

Also, the instructer has asked us to use the do/while loop in the program, so I was wondering does the loop go by word or sentance each time it is cycling through?

Take a look at an ascii chart to find out how you will tell if it is capital or not, and how to change the character. Note that you can treat chars like numbers.

The loop does whatever you tell it to. The way you wrote it, it goes through the input by a single character.

Some (correct ;)) modification of your original algorithm:

char c, prevc = ' ';
    while (cin.get(c)) {
        if (prevc == ' ' && islower((unsigned char)c)
            c = toupper((unsigned char)c);
        cout.put(prevc=c);
    }

First follow the Ancient Dragon comment.
then parse the whole string by checking each letter, run a for loop.
I dont know if your using c++ string or cstrings. I am explaining using cstring

char str[MAX];
cin.getline(str,MAX);
int len=strlen(str);
for(int i;i<len-1;i++)//run till lenght -1
{
if(str[i]=' ')
toupper(str[i+1]);
}
for(int i;i<len-1;i++)//No initialization of i
                                //you surely meant 
//for(int i=0; i<len  ;i++)              
{
if(str[i]=' ')//is this right?? and what if last character is a space.
.
.

Instead use strings.

Instead use strings.

Yeah, suerly I mean initialization.
As far as your second comment is concerned, look at the test condition, it says i<len-1 which means that the maximum value of i can be len-2.
So the loop will never come to the last character. Hence the i+1 will be actually the last character of string. Hence it is perfectly fine.

Though, I am sorry about the initialization part, I just wrote that outta hurry.

Ofcourse, I agree that using c++string is best.

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.