-2

Hey guys,

I've seen a lot of posts around the internet about this, but I still don't understand how to do that exactly. For some reason, I can only uppercase my char array until there is a space...

    #include "stdafx.h"
    #include <iostream>
    #include <string>


    std::string caps (char[]);


    int main() { 
        const int MAXLEN = 256;     
        char chaine[ MAXLEN ] = "";  

        //Here i am inputting my list of char into my array
        std::cout << "Chaîne ? ";
        std::cin >> chaine; 

        //Calling my caps function
        caps( chaine );

        // results
        std::cout << "Résultat: " << chaine << std::endl;

        _gettch();
        return 0;


    }


    std::string caps (char chaine []){


    std::string check=chaine;

    for (int i=0; i<=check.length(); i++){  //I added check.length() because it's the only way I know to check for the length of the array


    chaine[i]=toupper(chaine[i]);

    }

    return chaine;

    }

So let's say I write "Hey you" The output will be "HEY" and that's it. I am le confused. Thanks for the help!

user3232709
  • 7
  • 1
  • 2
  • 4
    That's because `std::cin >> wtv;` stops at whitespace. You should use [`getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) – Borgleader Jan 29 '14 at 07:04
  • Just take a look what you have in `chaine` after `std::cin >> chaine;`. That will give you an answer. P.S. At least you may use `gets` or it's analogs. – VP. Jan 29 '14 at 07:04
  • 2
    or use `std::noskipws` – James Jan 29 '14 at 07:05
  • 1
    @James: This is a common misconception. [`std::noskipws` is only for skipping leading whitespaces](http://stackoverflow.com/q/5654660/183120) and not the ones in between. – legends2k Jan 29 '14 at 07:20
  • @legends2k i didn't know that! `getline` it is, thanks for telling me that. – James Jan 29 '14 at 07:38

1 Answers1

1

Change your input reception to this:

    std::string chaine;
    std::cout << "Chaîne ? ";
    std::getline(std::cin, chaine);

    caps(chaine);             // <-- pass the name by const reference

This will work without skipping spaces. Now that you've an std::string, instead of creating another string inside caps, pass the input by reference i.e. change std::string caps (char chaine []) to std::string caps (const std::string &chaine).

legends2k
  • 31,634
  • 25
  • 118
  • 222