| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Syntax errors - Single app errors?!
I am new to programming so i dont really understand why this isn't working for me.
Any help would be brilliant. I have been given an application to make. The application is: "Write a program to read the number of units of electricity used by a consumer, and to write out the total cost, assuming the following rates : a. 5.2 p per unit for the first 100 units, b. 3.0 p per unit for the next 300 units, c. 1.4 p per unit for the remaining units." I have come up with the following solution; Code:
// Electricity consumed.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
int main() {
int units, hiprix, miprix;
float total;
printf(“please enter units:”);
scanf(“%d”, &units);
if(units <= 100) total = units*5.2;
else {
if(units <= 400) total = 520 + (units-100)*3.0;
else total = 520 + 900 + (units-400)*1.4;
}
printf(“\nThe total price is:%f Pounds”, total/100);
return 0;
}
this returns 8 errors and 3 warnings. I think it may have something to do with '"#include "stdafx.h" or #include "stdio.h""' maybe both, i am not sure if they are external libraries and that is my problem. If it is, how do I fix it? Thanks for any help. |
|
#2
|
||||
|
||||
|
You should probably include standard libraries like this:
#include <stdio.h> Besides that, please post the errors warnings you are getting. We cannot reproduce them since we can have different compilers (and compiler settings).
__________________
There is no such thing as C/C++, you either program C or C++ |
|
#3
|
|||
|
|||
|
Ok, i will try adding that library, should i take the other libraries out or just add the new one?
My errors are: Code:
error C2065: '“please' : undeclared identifier error C2146: syntax error : missing ')' before identifier 'enter' error C2059: syntax error : ')' error C2065: '“' : undeclared identifier error C2065: 'd”' : undeclared identifier warning C4244: '=' : conversion from 'double' to 'float', possible loss of data warning C4244: '=' : conversion from 'double' to 'float', possible loss of data warning C4244: '=' : conversion from 'double' to 'float', possible loss of data error C2017: illegal escape sequence error C2146: syntax error : missing ')' before identifier 'nThe' error C2059: syntax error : ')' 8 error(s), 3 warning(s) |
|
#4
|
||||
|
||||
|
There is no new library. I just suggested replacing your:
#include "stdio.h" with #include <stdio.h> Although this is probably not your problem and I would actually recommend using #include <cstdio> but I am not sure if you are programming in C or C++. I see that you are using weird characters though. Do not use “ nor ” for quoting your string literals but use ". So do not do: printf( “please enter units:” ); but use: printf( "please enter units:" ); |
|
#5
|
|||
|
|||
|
Ok, thanks. The app ran when i changed from “ to "
The application worked but the answer only flashes on then the application closes. What is the line i need to make it stay visible until a user presses a button? I tried what i thought it was but it has given me an error of: error C3861: 'system': identifier not found Anyway, this is what i thought it was Code:
system ("PAUSE");
return 0;
And I am programming (badly) in C++ *Edit* I have also tried void pause() That has taken the error out but it has not stopped my problem.. |
|
#6
|
||||
|
||||
|
If you program in C++ then forget about stdio.h. Try the C++ way with iostream.
system( "PAUSE" ) is a non-portable way of doing it, plus it uses an ugly system call. I guess you'd have to include stdlib.h to make it work? Not sure.. In c++ I just use: #include <iostream> std::cin.get(); |
|
#7
|
|||
|
|||
|
I have just tried:
#include <iostream> std::cin.get(); Using "std::cin.get();" has not given me an error but has not solved my problem. I have tried "system( "PAUSE" );" again after including iostream and this has solved my problem. My final code looks like this: Code:
// Electricity consumed.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
int main() {
int units, hiprix, miprix;
float total;
printf("please enter units:");
scanf("%d", &units);
if(units <= 100) total = units*5.2;
else {
if(units <= 400) total = 520 + (units-100)*3.0;
else total = 520 + 900 + (units-400)*1.4;
}
printf("\nThe total price is:%f Pounds", total/100);
system( "PAUSE" );
return 0;
}
This works but what (if any) improvements would you do. i.e. what is good practice to include. |
|
#8
|
|||||
|
|||||
|
Quick example of what I would more or less do:
cpp Code:
|
|
#9
|
|||
|
|||
|
Quote: Thanks for the information you have been a major help. I will try to write in a more lateral sense and think what will be running in the buffer at any one time. I will no doubt post a new thread in the near future with more rudimentary problems ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Syntax errors - Single app errors?! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|