Click here to Skip to main content
15,905,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I feel as if I have the source code correct for the most part. I prompt user for first answer, what is their age. it reads and stores correctly. I then prompt user for the second answer, how much money do they have. it also reads and store correctly. but when I get to the third answer which is a string and I used getline command. it seems like it just jumps to the next lines of code which prints everything out with the first two answers correct but the third wrong obviously. I'm not sure if there is a break or something I need to have in there? The code is as follows
#include "stdafx.h"
// Lab One Programming Exercise
// CS 1410
// your name
// CS 1410 -- your Section number
// --------------------------


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

int main()
{
	// declarations
	int age;
	float value;
	string name;

	// Prompt the user for their age
	cout << "\nPlease enter your age: ";
		
	// Get their age and store it in the variable age
	cin >> age;
	
	// Prompt the user for how much money they have
	cout << "\nHow much money do you have: ";
		
	// Get the amount of money and store it in the variable value
	cin >> value;
	
		// Prompt the user to enter their full name.
	cout << "\nPlease enter your full name:\n ";
		
	// Get their name and store it in the string variable name
	getline(cin, name);

	//    The person's name. You must display the full name
	cout << "Thank you, " << name << endl;
	
	//    The person's age
	cout << "You are " << age << "years old." << endl;
	
	//    The money the person has. Display a dollar sign and two digits 
	//    after the decimal point.
	cout << "and you have " << value << " in your pocket." << endl;
	
	cout << "\nGoodbye ...." << endl;
	cout << "\nPress Enter to continue...";
		cin.get();
}


What I have tried:

I'm new to programming but ive tried using the endl command at different points as well as \n. not sure how to get the name string to store and print correctly.
Posted
Updated 14-Jan-17 11:57am

1 solution

Your
C++
cin >> value;

do not clear the buffer and leave the trailing \n in the buffer.
and the getline consume the \n as the answer.
Try
C++
cin.ignore();
getline(cin, name);


Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900