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;
}

Recommended Answers

All 7 Replies

c = toupper(cin.get()); Or you could just do this: cout << toupper(c);

#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???

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.

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

commented: Missing <cctype>, but it is good to see the right approach for once. +3

All code given still has not worked..

All code given still has not worked..

Post what you have tried instead of just crying about it.

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.