grab first char of a string

Mar 12, 2019 at 4:27pm
Hi, I need help grabbing the very first character/letter of a string.

As a beginner, I cannot use arrays or anything "Fancy" for this.

I tried accepting the name as int, but that did not work. Here is how I tried that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

int main()
{
	int userName;
	char firstLetter, lastLetter;

	cout << "Please enter your first name: " << endl;
	cin >> userName;


	lastLetter = userName % 10;

	firstLetter = userName - lastLetter;
	if (firstLetter > 10){
		firstLetter = firstLetter / 10;

	cout << "The first letter of your first name is: " << firstLetter;

        }
return 0;
}


Any hints/tips would be helpful!
Last edited on Mar 12, 2019 at 4:28pm
Mar 12, 2019 at 4:46pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string userName;

	cout << "Please enter your first name: " << endl;
	cin >> userName;

	cout << "The first letter of your first name is: " << userName[0];

return 0;
}

Please enter your first name: 
Rachmainow
The first letter of your first name is: R 
Mar 12, 2019 at 4:52pm
I'm not sure if I am allowed to user userName[0]. Is this the only way?
Mar 12, 2019 at 5:10pm
I'm not sure

If you are not sure I regard it as your task to make it unmistakable clear what is allowed for you and what is not.

Any hints/tips would be helpful!

This was another one.
Mar 12, 2019 at 5:19pm
okay. I will rephrase.

I am NOT allowed to user userName[0], as I explained I am not allowed to use arrays or "anything "Fancy'"." :)

Thank you for the hint/tip that I specifically did NOT ask for.

Unless I am mistakenly naming userName[0] as an ARRAY.

I'm sure this tip will be helpful one day.
Mar 12, 2019 at 5:23pm
elevona wrote:
Unless I am mistakenly naming userName[0] as an ARRAY.


You are mistakenly naming userName[0] as an ARRAY.

Put a green tick on the question and move on.
Mar 12, 2019 at 5:27pm
closed account (E0p9LyTq)
Nothing fancy...no strings, no arrays, just a single char variable.

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

int main()
{
   char userName;

   std::cout << "Please enter your first name: ";
   std::cin >> userName;
   std::cout << '\n';

   std::cout << "The first letter of your name is: " << userName << '\n';
}
Please enter your first name: Joe d'Ragman

The first letter of your name is: J
Mar 12, 2019 at 5:28pm
If I insert in your program directly after cin a cout of the variable used in cin, like this:
1
2
	cin >> userName;
	cout << userName;

I get followin output:
Please enter your first name: 
Rachi
0 

So, the used variable does not contain my input. What type of variable are you allowed to receive input?
Mar 12, 2019 at 5:34pm
FurryGuy... Thank you so much! For some reason, I was INTENT on using a string to capture the information. Thank you for the clarification. This is very helpful and I will use it going forward.

MikeStgt... Thank you for explaining HOW to grab it from a string. This will be very helpful for future reference. You also pinpointed on how I was trying to grab it (using int), though I just needed to use a char and dump the rest of the data. Thanks!
Mar 12, 2019 at 5:55pm
closed account (E0p9LyTq)
Actually if you want to use a std::string for input you should use std::getline() instead of std::cin.

std::getline lets you retrieve the entire line of text, spaces and other delimiters. std::cin can still leave data in the stream if it was entered.

There are problems with std::cin:
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 your first name: ";
   std::string firstName;
   std::cin >> firstName;

   std::cout << "Enter your middle initial: ";
   std::string middleInitial;
   std::cin >> middleInitial;

   std::cout << "Enter your last name: ";
   std::string lastName;
   std::cin >> lastName;

   std::cout << '\n';

   std::cout << "Your full name is " << firstName << ' ' << middleInitial << ' ' << lastName << '\n';
}
Enter your first name: Joe D. Ragman
Enter your middle initial: Enter your last name:
Your full name is Joe D. Ragman
Enter your first name: Joe
Enter your middle initial: D.
Enter your last name: Ragman

Your full name is Joe D. Ragman

With std::string and std::getline:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
   std::cout << "Enter your full name: ";
   std::string fullName;
   std::getline(std::cin, fullName);

   std::cout << '\n';

   std::cout << "Your full name is: " << fullName << '\n';
}
Enter your full name: Joe D. Ragman

Your full name is: Joe D. Ragman

If you wanted to separate out the full name string into the individual parts you could use a std::stringstream and extract the full name into other strings as you do using std::cin, but that is something really FANCY.
https://en.cppreference.com/w/cpp/io/basic_stringstream
Mar 12, 2019 at 6:09pm
closed account (E0p9LyTq)
Another way to get around std::cin's rather simplistic extraction:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <limits>

int main()
{
   std::cout << "Enter your first name: ";
   std::string firstName;
   std::cin >> firstName;
   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

   std::cout << "Enter your middle initial: ";
   std::string middleInitial;
   std::cin >> middleInitial;
   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

   std::cout << "Enter your last name: ";
   std::string lastName;
   std::cin >> lastName;
   std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

   std::cout << '\n';

   std::cout << "Your full name is " << firstName << ' ' << middleInitial << ' ' << lastName << '\n';
}
Enter your first name: Joe D. Ragman
Enter your middle initial: D. Ragman
Enter your last name: Ragman

Your full name is Joe D. Ragman

This is fancy-dancy froo-froo! :)

http://www.cplusplus.com/reference/istream/istream/ignore/
http://www.cplusplus.com/reference/limits/numeric_limits/
http://www.cplusplus.com/reference/ios/streamsize/
Mar 13, 2019 at 12:00am
Thank you for that!

Does the null terminator completely wipe my keyboard buffer? It doesn't seem to be working for me!

THanks
Mar 13, 2019 at 1:48am
closed account (E0p9LyTq)
'\n' is not the null terminator, it is the newline/carriage return. The null terminator is '\0'.
Topic archived. No new replies allowed.