I'm working on this assignment for class and I've ran into a problem.
The objective of the program is to have the user to
- Input their first then last name.
- Then the program will ask to in put a nick name.
Finally the program will output your
- first name
- nick name all caps in quotes
- last name
a more detailed version of the instructions can be found here
So my problem lies on how will I manipulate a string to place the nickname in between the first and the last.
I'm using cin.getline() to input the first and last name at the beginning. I can't seem to find the last name in the string. Is there a way to read a string/char[] into another string after a certain location in the array.
like if there is a function in some c++ native library that went (pseudo code)
readFromPoint(array, otherArray, beginPoint, stopPoint);
or am i just going to have to build my own loop.
I would like to input my first and last name into one string. Then place them into two separate strings. Then I place (first, nickname, last) into the first and original string.
I know if I just used two cin statements it could save me a lot of heart ache. (I believe I've heard others call it dangerous)
Here's my code:
#include <iostream>
#include <cstring>
using namespace std;
char Name[46], // the full names leangth - 2
firstName[16],
lastName[16],
nickName[16];
int numOSpaces;
//void rearrangeName();
int FindSpace(string, int);
int FindNull(string);
int main(){
cout << "Pleast enter your first then last name...\n";
cin.getline(Name, 30);
int ender = FindSpace(Name, strlen(Name));
// here we check if you entered the right amount of spaces
while (numOSpaces > 1 || numOSpaces < 1){
if (numOSpaces > 1)
cout << "There are too many spaces only put one space:\n";
if (numOSpaces < 1)
cout << "Please separate your first and last name:\n";
cin.getline(Name, 30);
ender = FindSpace(Name, strlen(Name));
}
cout << "Hello " << Name <<".\n";
cout << "\n\nEnter your nickname...\n";\
cin.getline(nickName, 30);
cout << "Whats up " <<nickName << "!" << endl << endl;
// Now we separate the first name and the last name
//rearrangeName();
}
// used to tell where user stopped inputting into the string
int FindNull(string find){
int index = 0;
do{
index++;
}while (isprint(find[index])); // this stops at the null terminator
// }while (find[index] != '\0'); // as will this
return index;
}
// here we find the first space
int FindSpace(string mystring, int size){
int Space;
int spaceCount = 0;
for (int index; index < size; index++){
if (isspace(mystring[index])){
Space = index;
spaceCount++;
}
}
numOSpaces = spaceCount;
return Space;
}
void findName(string fullName, string otherName){
}
And I can't use any outside library of course if I want to turn this in that is. Only those native to C++.
std::stringwhich is part of the C++ standard library, and then it all becomes easy. Using raw char arrays in C++ is a terrible idea.char Name[46]et al. Why aren't you usestd::stringfor those as well, as you're using it already for your function parameters?