So, I'm not that new to C++ but I'm still learning some of the basics. I want to learn how to input multiple lines. It can be using a for loop or while loop.
> I thought that doing something like this would work, but I don't understand why not.
I don't understand you
your solution is incomplete, ¿so how did you figure out that it was wrong?
and if you figure it out that, ¿why you don't understand why it is wrong?
take a deck of cards
count how many cards are on the deck
¿how many times did you say "there are X cards"? ¿when did you say it?
¿which one was the bigger card? ¿how do you figure it out?
Start by writing pseudocode or a flow diagram, don't worry about c++ syntax yet.
I didn't say I knew it was right. I know is wrong because I can't get it right.
I'm trying to get the amount of lines a user enters and return it.
For example, the user enters,
April 2020
June 2020
December 20.5
I want to return the amount of lines entered. In this case this is 3 lines. I understood this part just a few minutes ago. Now the only problem I am having is returning the amount of integers in the string. I also managed to understand how to get the length of the biggest line.
So, for the case above, there are 3 lines and the longest line has 13 characters.
Now, I want to be able to also return the "n" amount of integers and that would be 2 instead of 3 because 20.5 is a float not an integer.
I'm sorry if I made it really bad for you to understand. Is just that I can't explain some of this things clearly.
separate your input by words
for each for word
try to convert it to an integer //stoi() or using stringstream
check if the conversion was successful and that the word was fully consumed
While atoi() can silently fail to convert the value, stoi() will throw an exception on conversion failure. It will not be able to tell you if the result you converted is "only" an int.
I'd recommend using either stringstreams or some of the std::string find functions, or some combination of the two, or perhaps some form of regex.