Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C++
// 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;
}
Posted
Updated 18-Jul-18 21:27pm
v3
Comments
[no name] 17-Jul-18 16:21pm    
You only get the default value when you enter 'car' which is case 1. It then performs the 'cout << "car toll is 0.50";' and the method exits immediately. I think you are missing a While Loop
[no name] 17-Jul-18 16:35pm    
The code so far Looks ok. Are you sure you enter a number - and not that your num lock is off?

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

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.
 
Share this answer
 
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:
C++
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.
 
Share this answer
 
Quote:
I was expecting a toll of 0.5 when user puts car and so on
You should expect a toll of 0.5 when the user puts 1. Computers are very precise beasts.
 
Share this answer
 
Comments
Richard MacCutchan 19-Jul-18 3:43am    
Except when they give the wrong answer :)
Richard Deeming 19-Jul-18 10:13am    
Unless you're running a very old Pentium, they almost always give the right answer. Just not necessarily to the question you though you'd asked. :)
Richard MacCutchan 19-Jul-18 10:20am    
So very true.

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