I have a program where the cout "ing" a string misses the beginning letter of the string. I have made it so that there are 3 options to input/output the information. The "missing" letter in the string is ONLY evident in option 1; option 2 and option 3 does not have any issues. What am I doing wrong?

Here is my code:

// Joseph Yong
// CSC2430
// Homework_6

//Header File
/*--------------------------------------------------------------------------*/

#include <iostream>
#include <string>
using namespace std;

class Bible
{
public:
	//Constructors
	Bible();
	Bible(string nam, int chapt, int vers, string testa, string txt);

	//Mutators
	void setName(string nam);
	void setChapter(int chapt);
	void setVerse(int vers);
	void setTestament(string testa);
	void setText(string txt);
	void setSetup1(string nam, int chapt, int vers, string testa);
	void setInput();

	//Accessors
	string getName();
	int getChapter();
	int getVerse();
	string getTestament();
	string getText();

	//Output
	void write1();
	void write2();

private:
	string name, testament, text;
	int chapter, verse;
};
#include <iostream>
#include <iomanip>
#include <string>
#include "Bible.h"

using namespace std;

int main()
{
	Bible bib;
	int answer;
	string nam, testa, txt;
	int chapt, vers; 

	cout << "How would you like to enter in the information for the Bible verse? (1, 2, or 3): " << endl << setw(7) << "1) One by one" <<
		endl << setw(7) << "2) All at once" << endl << "3) Everything without the verse contents" << endl;
	cin >> answer;

	if(answer == 1)
	{
		cout << "\n1) One at a time" << endl;
		cout << "Please enter the name of the book: ";
		cin.ignore();
		getline(cin, nam);
		bib.setName(nam);
		bib.getName();
		cout << "Please enter the chapter of the book: ";
		cin >> chapt;
		bib.setChapter(chapt);
		bib.getChapter();
		cout << "Please enter the verse of the chapter: ";
		cin >> vers;
		bib.setVerse(vers);
		bib.getVerse();
		cout << "Please enter the testament of the book: ";
		cin.ignore();
		getline(cin, testa);
		bib.setTestament(testa);
		bib.getTestament();
		cout << "Please enter the contents of the verse: ";
		cin.ignore();
		getline(cin, txt);
		bib.setText(txt);
		bib.getText();

		cout << "\n\n";
		bib.write1();
		cout << "\n\n";
	}
		else if(answer == 2)
		{
			cout << "\n2) All at once" << endl;
			bib.setInput();

			cout << "\n\n";
			bib.write1();
			cout << "\n\n";
		}
			else if(answer == 3)
			{
				cout << "\n3) Everything without the verse contents" << endl;
				cout << "Please enter the name of the book: ";
				cin.ignore();
				getline(cin, nam);
				cout << "Please enter the chapter of the book: ";
				cin >> chapt;
				cout << "Please enter the verse of the chapter: ";
				cin >> vers;
				cout << "Please enter the testament of the book: ";
				cin.ignore();
				getline(cin, testa);
				bib.setSetup1(nam, chapt, vers, testa);
				bib.getName();
				bib.getChapter();
				bib.getVerse();
				bib.getTestament();

				cout << "\n\n";
				bib.write1();
				cout << "\n\n";
			}
	return 0;
}
// Joseph Yong
// CSC2430
// Homework 6

//Implementation File
/*----------------------------------------------------------------*/
#include <iostream>
#include <string>
#include "Bible.h"

using namespace std;

//Constructors
Bible::Bible()
{
	name = "";
	chapter = 0;
	verse = 0;
	testament = "";
	text = "";
}

Bible::Bible(string nam, int chapt, int vers, string testa, string txt) //Initialize to any size
{
	name = nam;
	chapter = chapt;
	verse = vers;
	testament = testa;
	text = txt;
}

//Mutators
void Bible::setName(string nam)
{
	name = nam;						//Change the string value of name
}

void Bible::setChapter(int chapt)
{
	chapter = chapt;				//Change the int value of chapter
}
	
void Bible::setVerse(int vers)
{
	verse = vers;					//Change the int value of verse
}

void Bible::setTestament(string testa)
{
	testament = testa;				//Change the string value of testament
}

void Bible::setText(string txt)
{
	text = txt;						//Change the string value of text
}

void Bible::setSetup1(string nam, int chapt, int vers, string testa)
{
	name = nam;
	chapter = chapt;				//Changes all data members
	verse = vers;
	testament = testa;				
}

void Bible::setInput()
{
	cout << "Enter the book name, chapter, and verse: ";
	cin >> name >> chapter >> verse;
	cout << "Enter in the testament: ";
	cin.ignore();
	getline(cin, testament);
	cout << "Enter in the contents of the verse: ";
	getline(cin, text);
}

//Accessors
string Bible::getName()
{
	return name;
}

int Bible::getChapter()
{
	return chapter;
}

int Bible::getVerse()
{
	return verse;
}

string Bible::getTestament()
{
	return testament;
}

string Bible::getText()
{
	return text;
}



void Bible::write1()
{
	cout << name << " " << chapter << ":" << verse << " " << testament << endl;
	cout << text;
}

Dani AI

Generated

The missing first letter is coming from an extra cin.ignore() call. After getline(testament) the newline has already been consumed, so the following cin.ignore() discards the first character of the verse text. correctly pointed this out and confirmed removing the stray cin.ignore() fixes the output.

Mixing operator>> and getline is the usual source of this kind of bug: operator>> leaves the trailing newline in the input buffer, and a single cin.ignore() is appropriate to drop that newline — but calling cin.ignore() when there is no leftover newline will eat real input. Two safe patterns are (a) always clear the rest of the line after formatted extraction with a full discard, or (b) use getline everywhere and convert numeric input from strings. Example of (a):

#include <limits>

// after using operator>>
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // flush to end of line
std::getline(std::cin, text); // now safe to read the full verse line

As an alternative, std::getline(std::cin >> std::ws, text); will skip leading whitespace before the line read. Also note small cleanups: avoid calling getters immediately after setters unless using the returned value, and avoid using operator>> for book names that may contain spaces (use getline instead). These adjustments make input behavior predictable and prevent the first-character loss experienced in option 1.

Recommended Answers

All 3 Replies

Here is my output for option 1:

How would you like to enter in the information for the Bible verse? (1, 2, or 3)
:
1) One by one
2) All at once
3) Everything without the verse contents
1

1) One at a time
Please enter the name of the book: John
Please enter the chapter of the book: 1
Please enter the verse of the chapter: 1
Please enter the testament of the book: New Testament
Please enter the contents of the verse: Test is the test is the test


John 1:1 New Testament
est is the test is the test

Press any key to continue . . .

I think one of the cin.ignores throws the first letter of "Test is the test is the test" away. Please try to reproduce the error with minimum amount of code so people can quickly find what's wrong.

That did it! Thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.