4

When i try to output the string it doesnt output the text after the space. It should ask for student name and then output it when asked. This is C++. I have no more info to give but the site wont let me post it so this sentence is here.

/***************************************************/
/* Author:     Sam LaManna                         */
/* Course:     CSC 135 Lisa Frye                   */
/* Assignment: Program 4 Grade Average             */
/* Due Date:   10/10/11                            */
/* Filename:   program4.cpp                        */
/* Purpose:    Write a program that will process   */
/*             students are their grades. It will  */
/*             also read in 10 test scores and     */
/*             compute their average               */
/***************************************************/

#include <iostream>     //Basic input/output
#include <iomanip>      //Manipulators

using namespace std;

string studname ();     //Function declaration for getting students name

int main()
{
  string studentname = "a";     //Define Var for storing students name

  studentname = studname ();    //Store value from function for students name

  cout << "\n" << "Student name is: " <<studentname << "\n" << "\n";     //String output test

  return 0;
}

/***************************************************/
/* Name: studname                                  */
/* Description: Get student's first and last name  */
/* Paramerters: N/A                                */
/* Return Value: studname                          */
/***************************************************/

string studname()
{
  string studname = "default";


  cout << "Please enther the students name: ";
  cin >> studname;

  return studname;
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Sam LaManna
  • 425
  • 2
  • 7
  • 15
  • possible duplicate: http://stackoverflow.com/questions/8052009/returning-a-string (same problem, different context) – IronMensan Nov 09 '11 at 19:11

5 Answers5

5

You should use the getline() function, not the simple cin, for cin only gets the string before white space.

istream& getline ( istream& is, string& str, char delim ); 

istream& getline ( istream& is, string& str );

Extracts characters from is and stores them into str until a delimiter character is found.

The delimiter character is delim for the first function version, and '\n' (newline character) for the second. The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.

If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.

rcollyer
  • 10,475
  • 4
  • 48
  • 75
Crazy Bear
  • 101
  • 4
4

you can use getline so like this

string abc;
cout<<"Enter Name";
getline(cin,abc);
cout<<abc;

Getline

Chris Condy
  • 626
  • 2
  • 9
  • 25
3

Another alternative is to use the std::strings getline() function like this

getline(cin, studname);

This will get the whole line and strip of the newline. But any leading/trailing spaces will be in your string.

Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
2

cin likes to break things up by whitespace, so that is why you are only getting one name. Likely, since the assignment is telling you to grab the first and last name, you can likely assume that these will be separated by whitespace. In this case, you can just grab both separately, then concatenate them:

string firstname = "default";
string lastname = "default";

cin >> firstname >> lastname;

return firstname + " " + lastname;
Donald Miner
  • 38,889
  • 8
  • 95
  • 118
0

To get an entire line, you'll want to use getline instead of >>:

getline(cin, myString);
JoeFish
  • 3,009
  • 1
  • 18
  • 23