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:
| |
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):
| |
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
| |
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:
| |
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
| |
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!!
| |
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.
| |
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