How to input two words with space b/t??

My program runs fine. The only problem I have is when my program prompts for an animal name it wigs out when I enter two words... such as "Black Bear". I am trying to utilize a character array in place of "string word" in order to this. I realize that I will need to use the cin.peek / strcopy commands, but I am unsure how to do it.

Any assistance would be appreciated.

Chris


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>

using namespace std;

struct noahark
{
	int count;					
	string word;    //this needs to be a character array
	double charge;
	noahark *link;  //pointer to the next record

};

noahark * HeadPointer = NULL;  

//Declare function prototypes:

void InsertItem ( int, string, double, noahark * ); 
void PrintList (noahark * );

int main (  )
{
	int InitCount = 0;					
	string InitWord = "\0";
	double InitCharge = 0;

	noahark * CurrentRecordPointer = NULL;

	//for the while loop
	char ContinuationFlag = 'Y';

	while ( toupper (ContinuationFlag) == 'Y' )
	{
		cout << "ID Number of the animal: " << endl;
		cin  >> InitCount;

		cout << "Name of the animal: " << endl;
		cin  >> InitWord;

		cout << "Charge Fee of the Animal: " << endl;
		cin  >> InitCharge;

		CurrentRecordPointer = new noahark;

		//call the function to create the Linked List.

		InsertItem( InitCount, InitWord, InitCharge, CurrentRecordPointer );
		//demotes CurrentRecordPointer to HeadPointer
		HeadPointer = CurrentRecordPointer;

		cout << "Do you wish to enter any more items?" << endl;
		cin  >> ContinuationFlag;

	}  //end of while loop

	//call display function

	PrintList ( HeadPointer );
	system("pause");
	return 0;
}//end of main


void InsertItem ( int InitCount, string InitWord, double InitCharge,  noahark * CurrentRecordPointer)
{
	CurrentRecordPointer->count = InitCount;
	CurrentRecordPointer->word = InitWord;
	CurrentRecordPointer->charge = InitCharge;
	CurrentRecordPointer->link = HeadPointer;
}
void PrintList ( noahark * Head )
{
	while ( Head != NULL )
	{
		cout << Head->count  << "  " << endl;
		cout << Head->word   << "  " << endl;
		cout << Head->charge << "  " << endl;
		Head = Head->link;  //gets us to the next record
	}
}
This should be help.. Check out this. It lets you type words with spaces.
1
2
3
4
5
6
7
8
9
10
#include <string>
#include <iostream>
 
int main()
{
    std::string name;
    std::cout << "What is your name? ";
    std::getline(std::cin, name);
    std::cout << "Hello " << name << ", nice to meet you.";
}


Output:
What is your name? John Q. Public
Hello John Q. Public, nice to meet you
I need to use a character array and be able to input/output multiple items.... I understand the getline command with a basic program, but can't figure it out using linked list. Any help would be appreciated. Thanks.
Last edited on
Topic archived. No new replies allowed.