banner



How To Make Vector Elements Change Case C++

Hi everybody,

So got some other challenge on my hands. I need to read all the inputs from cin and store those values in to a vector. Then I need to change all the vector values to uppercase and print results eight words at a fourth dimension.

I have got the following for inputting the cord elements in to a vector:

                        ane
ii
iii
4
five
6
seven
8
ix
ten
11
12
xiii
fourteen
xv
16
17
                                                  #include <iostream>                          #include <cord>                          #include <vector>                          using                          std::string;                          using                          std::vector;                          using                          std::cout;                          using                          std::cin;                          using                          std::endl;                          int                          main () {     vector<string> v1;                          // Create an empty vector                          string words;                          // Create a string "words"                          while                          (cin >> words) {         v1.push_back(words);         }                          return                          0; }                      

This works just fine. But then to alter toupper I tried the post-obit but it doesn't piece of work (every bit I'm certain you'll tell is obvious):

                        1
two
3
4
5
6
7
8
9
ten
11
12
13
xiv
15
16
17
xviii
19
                                                  #include <iostream>                          #include <string>                          #include <vector>                          using                          std::string;                          using                          std::vector;                          using                          std::cout;                          using                          std::cin;                          using                          std::endl;                          int                          main () {     vector<string> v1;                          // Create an empty vector                          string words;                          // Create a string "words"                          while                          (cin >> words) {         v1.push_back(words);         }                          for                          (motorcar                          &due east : v1)                          // for all elements "e" in vector v1                          e = toupper(e);                          return                          0; }                      

The fault is "No matching role to call for 'toupper'. However I now take no idea how to solve this upshot.

Assistance is appreciated!!

Thanks!

Hi there,

just a little hint here: toupper workson chars, non std::strings.
Luckily, std::string's are made up of char's, so if you iterate them and alter every character to upper, that should work.

Yous may also want to check out the std::for_each algorithm, which allows to brand a change to every item of a container (remember that a std::string is basically a container of chars).

Promise that helps you lot, please do go back to us if you need any further assist.

All the best,
NwN

Try the following

                        one
2
iii
4
                                                  for                          (                          auto                          &due south : v1 ) {                          for                          (                          auto                          &c : south ) c = toupper( c ); }                      

Ok, then the error I was getting was then considering I was trying to use a graphic symbol conversion function on an unabridged string rather than iterating through which was then done past doing the range for part. It works not bad!

First though, vlad, could you maybe elaborate on how that role works? As I understand information technology it is using &s to reference characters in the vector v1 and then using &c to iterate through the characters in the string s, and and then changing each character toupper. Is that roughly correct?

I at present however have a big problem, because the next part of the question I take never seen done before:

It wants me to print 8 words per line. I have NO clue how to solve that cookie! Really clueless! I'd like to keep it using elementary coding considering that's where I am in the volume so far.

Here'southward the code as it stands now:

                        1
two
3
iv
five
6
7
8
9
10
11
12
13
14
15
16
17
xviii
nineteen
20
21
22
                                                  #include <iostream>                          #include <cord>                          #include <vector>                          using                          std::string;                          using                          std::vector;                          using                          std::cout;                          using                          std::cin;                          using                          std::endl;                          int                          main () {     vector<string> v1;                          // Create an empty vector                          string words;                          // Create a cord "words"                          while                          (cin >> words) {         v1.push_back(words);         }                          for                          (motorcar                          &due south : v1) {                          for                          (auto                          &c : s) c = toupper(c);         cout << s <<                          " ";         cout << endl;     }                          render                          0; }                      

Kickoff though, vlad, could you lot maybe elaborate on how that function works? Equally I understand it it is using &s to reference strings in the vector v1 and and so using &c to iterate through the characters in the string s, then changing each character toupper. Is that roughly right?

Right

Do you know how to utilize stringstreams? They're pretty useful to read strings one discussion at a fourth dimension

Final edited on

Hi maeriden,

No, oasis't got that far withal I guess :p This is week 3 of C++ coding self-taught through a book, then still learning the 1+1's unfortunately.

Actually there is no need for them, I disregarded that every chemical element in the vector is a single discussion
Information technology all boils downward to: print a word and a space 8 times, print a newline, eight words, newline, etc
Use an int to continue track of how many words you have printed on every line. When that number is a multiple of 8 impress the newline (this check is done using the modulo operator. If you're not familiar with it you can also check when it equals 8 and so gear up it back to 0)

I won't post the code, every bit information technology is an exercise, but if y'all post yours I can review it

                      1
two
3
4
v
6
7
8
ix
10
11
                                              const                        std::vector<std::string>::size_type Northward = 8; std::vector<std::cord>::size_type i = 0;                        for                        (                        const                        auto                        &s : v1 ) {       std::cout << s;                        if                        ( ++i % N == Due north - one ) std::cout << std::endl;                        else                        std::cout <<                        ' '; }                        if                        ( i % N != N = one ) std:;cout << std::endl;                    

Final edited on

Ok, sorry vlad, trying to gun this one by myself so this won't reflect your lawmaking. It looks like I am getting closer, but this code is just repeating the judgement twice, once on 1 line so the residuum one word per line.

In what ways tin I tweak this existng code a little to go the result I need?

Thank you for all the help! It is through engaging with folks similar yourselves that I really am starting to experience similar I am getting a basic agreement! Thanks a lot!!

                        one
ii
3
4
5
6
7
viii
ix
10
eleven
12
13
fourteen
fifteen
16
17
18
xix
20
21
22
23
24
                                                  #include <iostream>                          #include <string>                          #include <vector>                          using                          std::string;                          using                          std::vector;                          using                          std::cout;                          using                          std::cin;                          using                          std::endl;                          int                          main () {     vector<string> v1;                          // Create an empty vector                          string words;                          // Create a string "words"                          while                          (cin >> words) {         v1.push_back(words);     }                          for                          (auto                          &s : v1) {                          for                          (auto                          &c : south) c = toupper(c);         cout << s <<                          " ";     }                          for                          (decltype(v1.size()) i = 0; i != 8; ++i)         cout << v1[i] <<                          '\n';     cout << endl;                          render                          0; }                      

I showed yous an example how to do this task. Please investigate the case. It is not interesting to uodate your own errors. Try to exercise it yourself.

Last edited on

A more than fair point vlad, my apologies!

I was having a look at your case and I'm not quite sure I follow... why is there the modulo there during the iteration? A trouble I also seem to be having is where exactly to fit the code in, but that I can figure out through a piddling trial and mistake.

The other issue I'm getting is the first 2 lines... tin a vector be initialised with an "="? It is giving me an error. Would defining it as only normal variables piece of work the same manner?

On the last line information technology gives me an error to say that "expression is not assignable"

Last edited on

Ok, latest update, I have stared and tried and tried at this thing for over half-dozen hours now and I all the same can't become it to work. I can get words shoved together, I tin somehow get it to print every second give-and-take in a sentence (not sure how), I can go it to print a bunch of upside-downward question marks, but nix that I need.

I tried the example lawmaking above from vlad, but it merely gives me a whole agglomeration of compile errors. I tried variations which compiled, but didn't give what I wanted unfortunately.

Delight, if anybody has some more than insights! I'm really ripping my pilus out!!

I get how to print eight elements of the cord. That part is fine. My problem is how do I get it to print the next 8 on a new line and and then-on and so-on. The claiming for me is the new line thing.

                      1
2
3
4
5
six
7
8
9
10
11
12
13
fourteen
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
                                              #include <iostream>                        #include <cord>                        #include <vector>                        using                        std::string;                        using                        std::vector;                        using                        std::cout;                        using                        std::cin;                        using                        std::endl;                        int                        chief () {     vector<string> v1;                        // Create an empty vector                        string words;                        // Create a string "words"                        while                        (cin >> words) {         v1.push_back(words);     }                        for                        (auto                        &s : v1) {                        for                        (auto                        &c : due south) c = toupper(c);         cout << southward <<                        " ";     }     cout <<                        '\northward'                        ;                        const                        unsigned                        perLine = eight ;                        decltype(v1.size()) wordCount ;                        for                        ( wordCount = 0; wordCount != v1.size() ; ++wordCount )     {         std::cout << v1[wordCount] <<                        ' '                        ;                        if                        ( (wordCount + 1) % perLine == 0 )                        // evenly divisible?                        std::cout <<                        '\n'                        ;     }                        if                        ( wordCount % perLine != 0 )         std::cout <<                        '\due north'                        ;                        return                        0; }                    

fire, yous are an absolute fable!! It has got it working perfectly!! Just had to remove the "cout << s << " ";" and then information technology worked just perfectly.

Information technology is going to take me a little time to fully look through and sympathize what you did. Adding a few breakpoints here and there has helped my dissect the code. Generally I get it though...

Just the decltype statement, is that taking the size of v1 and putting information technology in to a value "wordCount"?

The other thing I find interesting is the (word count + i) , why can't ane use ++wordCount? (I tried it and it jumps every 2nd letter of the alphabet.)

Equally I understand it the modulo % divides a value and returns true if it has no decimal points... then how exactly does 8 % 8 == 0 work? Is it because that is the only number that 8 tin divide evenly? doesn't 0 reverberate false bool and ane equal true, or have I got them mixed up?

Last affair, the very terminal if statement, is that actually necessary? It seems to me to be a redundant piece of lawmaking?

A really huge THANK You lot!! My brain was really beginning to spin!

Simply the decltype statement, is that taking the size of v1 and putting it in to a value "wordCount"?

Since I extracted it from your code, I assumed you knew what it was doing. decltype returns the blazon of an expression. In this particular example the blazon of the value returned past v1.size(), and then that wordCount is given that type.

The other thing I notice interesting is the (word count + 1) , why can't one use ++wordCount? (I tried it and information technology jumps every second letter.)

Because the point of the code is not to increment wordCount, but to use wordCount+1. wordCount is already incremented every iteration of the for loop. wordCount+one must be used because the index values for arrays/vectors are 0 to size-1, and it'southward required for the modulus to work the mode it should.

As I understand it the modulo % divides a value and returns truthful if it has no decimal points... so how exactly does 8 % 8 == 0 work?

Y'all sympathise information technology incorrectly. Modulo returns the remainder of a division operation. So, 8 % viii is 0 and 7 % eight is vii.

Last thing, the very terminal if statement, is that really necessary? Information technology seems to me to be a redundant slice of code?

It's simply necessary if you want to consistently end on a new line.

Terminal edited on

Ahhh!! Ok! It all makes sense now!

You can clearly encounter why I was struggling to reply the question!!

Give thanks you! This really helps me a bucket load!!

Topic archived. No new replies allowed.

Source: http://www.cplusplus.com/forum/beginner/89508/

Posted by: smithfoure1955.blogspot.com

0 Response to "How To Make Vector Elements Change Case C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel