Question

So Im here and im stuck. I need to figure out how to get the character of the first name to be printed on a separate line and the middle and last. is there a way where i can tell a my function to stop once it hit " "? Please help.

I #include iostream 2 #include string using namespace i; string name 7 getline cin, name 4 int main ) cout<Please enter yourproblem3.cpp 3Write a program which asks the user to enter their full name (First Middle Last, using getline) and then prints

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* Approach is to print the whole string character wise and if any space(' ') occurs then we have to print newline.

We have advantage in using string class that we can also traverse it character wise that's why you have to use this approach.

Futher explanation i have given in below code comments;

output picture is at last.

And below program is working perfectly.

*/

#include<iostream>

#include<string>

using namespace std;

int main(){

string name;

cout<<"Enter your full name"<<endl;

getline(cin,name); // your enter your name;

int i=0; //iterator to start from 0th character.

while(name[i]!=NULL){ // if it reaches to end of sentence that is NULL character then while loop stops.
  
   if(name[i]==' '){ // if space character is there then we print new line.
       cout<<endl;   
  
   }else{ // if its other than space character then it will print that character.
           cout<<name[i];   
  
   }
  
   i++; //we are increasing iterator to visit every character.

// output pic is below.
      
}


return 0;
}

/* Here i entered a full name as "first middle last" and you can see it printing perfectly.

Enter your full name first middle last first middle last Process exited after 11.23 seconds with return value e Press any key */

Add a comment
Know the answer?
Add Answer to:
So Im here and im stuck. I need to figure out how to get the character...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • below i added the code, but while running it, makefile.win error showing. how i fix this?...

    below i added the code, but while running it, makefile.win error showing. how i fix this? 1.)waitlist.h #include<iostream> using namespace std; class guest{    public:    string name;    guest*next;    guest*prev;    guest(string);    }; class waitList{    int numberOfGuests;    guest* first;    guest* last;    public:        waitList();        void addPerson(string);        void deletePerson(string);        guest* getFirst();        guest* getLast();    }; 2.)waitlist.cpp #include<iostream> #include"waitlist.h" using namespace std; guest::guest(string name){    this->name...

  • problem2.cpp Write a program vhich asks the user to enter their full name (First Ni ddle...

    problem2.cpp Write a program vhich asks the user to enter their full name (First Ni ddle Last, using getline()) and then ei ther either prints out their middle name, or prints "Error: no midd le name" if they entered a name vithout a middle name (1ess than tvo spaces). 6 8#include <iostream> 9 #include <string 18 using namespace std; 11

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string...

    Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string first, last, job;    double hours, wages, net, gross, tax, taxrate = .40;    double oPay, oHours;    int deductions;    // input section    cout << "Enter First Name: ";    cin >> first;    cout << "Enter Last Name: ";    cin >> last;    cin.ignore();    cout << "Enter Job Title: ";    getline(cin, job);    cout << "Enter Hours Worked:...

  • Code is in C++: Im wondering how i can make the program continue to ask the...

    Code is in C++: Im wondering how i can make the program continue to ask the user to enter Y/N with the if statment. Is it possible with an If statment? #include <iostream> using namespace std; int main() { float usDollars,cYuan; float *Dollars; char choice; Dollars = &usDollars; while(usDollars >= 0){ cout <<"Enter the amount in U.S Dollars: "; cin >> usDollars; cout << usDollars<< " U.S Dollar in Chinese Yuan is :"<<*Dollars*7.09<<endl; if (choice == 'y' || choice ==...

  • How could I separate the following code to where I have a gradebook.cpp and gradebook.h file?...

    How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...

  • I need to get this two last parts of my project done by tonight. If you...

    I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT