如何才能正确编译? [英] How do I get this to compile correctly?

查看:74
本文介绍了如何才能正确编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得好像我的源代码大部分是正确的。我提示用户第一个答案,他们的年龄是多少。它正确读取和存储。然后我提示用户第二个答案,他们有多少钱。它还可以正确读取和存储。但当我得到第三个答案,这是一个字符串,我使用了getline命令。它似乎只是跳转到下一行代码,打印出前两个答案正确但第三个错误显然是正确的。我不确定那里是否有休息或者我需要的东西?代码如下

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();
}





我的尝试:



我是编程新手,但我尝试在不同的点和\ n使用endl命令。不确定如何正确存储和打印名称字符串。



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.

推荐答案

你的

Your
cin >> value;



不清除缓冲区并留下尾随 \ n 在缓冲区中。

getline 使用 \ n 作为答案。

尝试


do not clear the buffer and leave the trailing \n in the buffer.
and the getline consume the \n as the answer.
Try

cin.ignore();
getline(cin, name);





使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。



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.


这篇关于如何才能正确编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆