I have the following code which I have been trying get to output ALL capital letters using the toupper function.

My code will build successfully, however all the letters are still lower case when ever I type.

PLEASE HELP ME UNDERSTAND WHAT I AM DOING WRONG!

The following is the code I have gotten to so far, but I need to know where to place the toupper function. Everytime I try to associate it with char c I do something wrong.
-------------------------------------------------------------------------------------------

#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() ;
	} while ( !cin.eof()) ;
	return 0;
}

Edited 7 Years Ago by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

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

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

Changed code,added "c = toupper(cin.get());" I believe I have tried this one before too. The program is still just simply typing out lowercase letters only!!!

This one is really frustrating me, I think that I am thinking too hard about it though.

Any other pointers???

Edited 7 Years Ago by WaltP: And again, Added CODE tags -- look them up!

Remove that line again and put it inside your loop. Put a cout << c; inside your loop and remove the other cin/cout statements.
So your loop-block should look like this:

{
    c = toupper(cin.get());
    cout << c;
}

also code-tags. Learn to use them.

Edited 7 Years Ago by Nick Evan: n/a

You can also use this code snippet if you are confused..
char response[256];
cout<<"Enter a word.."<<endl;
---------------------------------------------
<cin.getline(response,256);
for(int i=0;i<response.length();i++){
response=toupper(response);
}>
-----------------------------------
This code converts 'response' to capital letters.
This is just for the knowing,anyway.

Or a real C++ solution:

#include <iostream>
#include <algorithm>
#include <string>

struct upper {
  int operator() ( int c )  
  {
      return toupper ( static_cast<unsigned char> ( c ) );
  }
  
};

int main(){
    std::string str = "";
    std::getline(std::cin, str);
    std::transform(str.begin(),str.end(),str.begin(),upper());
    std::cout << str;
    return 0;
}

@tkud: code-tags: learn to use them

Comments
Missing , but it is good to see the right approach for once.
This article has been dead for over six months. Start a new discussion instead.