C ++代码切换无法正常工作 [英] C++ code switch not working

查看:100
本文介绍了C ++代码切换无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的课本逐字逐句地尝试了这个代码,但是我无法得到正确的答案,cout只显示默认值,当用户放车等时我预计收费为0.5。请帮助,我会很感激你的帮助。如果一个用户进入汽车,它应该显示0.50的收费,如果它是一个总线,那么它应该显示1.50的收费等等。我也删除了==并将其改为=但是这样做了没有帮助,我将如何实现这一目标,谢谢你的帮助。下面是代码



我尝试过:



i tried this code from my text book word for word but i cant get the correct answer,the cout just shows the default,I was expecting a toll of 0.5 when user puts car and so on.Please help,I would appreciate your help.If a user enters car then it should display toll of 0.50 and if it is a bus then it should show toll of 1.50 and so on.I have also removed "==" and changed it to "=" but that does not help,how would i achieve this ,thanks for help.Below is the code

What I have tried:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;
int vehicleclass;
float toll;
//ofstream out(test.txt)

int main()
{
	//ofstream out(test.txt);
	int vehicleclass;
	double toll;

	cout << "enter vehicle details" << endl;
	
	cin >> vehicleclass;
	switch (vehicleclass)
	{
	case 1:
		cout << "car toll is 0.50";
		toll = 0.50;
		break;
	case 2:

		cout << "bus";
		toll = 1.50;
		break;

	case 3:
		cout << "truck";
		toll = 2.00;
		
		
		break;
		default:
			cout<<"error"<<endl;
	}
    return 0;
}

推荐答案

Quote:

我从我的课本中逐字逐句地尝试了这段代码,但我无法得到正确的答案,

i tried this code from my text book word for word but i cant get the correct answer,



这不是你的代码,它不会给你预期的结果而你不会理解为什么。

是时候学习调试器了。



有一个几乎通用的解决方案:在调试器上逐步运行代码步骤,检查变量。

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

没有魔法调试器,它不知道你应该做什么,它没有找到错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



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



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

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。


This is not your code, it din't give you expected result and you don't understand why.
It is time to learn the debugger.

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


此代码缺少错误处理和更多功能。如上所述 - 使用调试器。



有用的是总是在错误的情况下输出有用的数据,例如:

This code is missing the error handling and more functionality. As written - use the debugger.

Helpful is always to make an output in error cases with useful data like:
default:
  cout<<"error"<<endl;
  cout<<"vehicleclass "<< vehicleclass << " not handled" << endl;
  break;

我猜输入是一些字符串导致数字无效。

I guess that the input was some string which is resulting in an invalid number.


引用:

当用户放车等时,我预计收费为0.5

I was expecting a toll of 0.5 when user puts car and so on

您应该会收到当用户输入 1 时, 0.5 。计算机是非常精确的野兽。

You should expect a toll of 0.5 when the user puts 1. Computers are very precise beasts.


这篇关于C ++代码切换无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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