Dont Work The numbers

Hello again, I dont know how to:

I put a sentence:
*Calculate the lenght of it*
Get the first letter

I dont know how to: if i have 17 letters, how to go something like: 0,1,2,3,...17 ??
Everytime is random.

Thanks in advance
I really need to go to sleep xD, i want to finish this xD

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main()
{
    std::string w;
    std::cout << "Enter a string: ";
    std::getline (std::cin,w);
    char c = w[w.size];                //How to get to THAT number?

    return 0;
}
You're not terribly far off. size needs to have parentheses after it as it's a member function, and remember that indexes only go from 0 to size()-1, inclusive.

-Albatross
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

int main()
{
   std::cout << "Enter a string: ";
   std::string w;
   std::getline (std::cin, w);

   int string_size = w.size();
   std::cout << "The size of the string is: " << string_size << "\n";

   string_size = w.length();
   std::cout << "The length of the string is: " << string_size << "\n\n";

   for (unsigned int i = 0; i < w.size(); i++)
   {
      std::cout << w[i] << " ";
   }
   std::cout << "\n";
}}


PLEASE learn to use the reference section here, a lot of your questions would be answered. With examples.
http://www.cplusplus.com/reference/string/basic_string/size/
Last edited on
Solved in another way, i forget to put something, thanks anyways.
Topic archived. No new replies allowed.