C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 3rd, 2009, 08:14 AM
dmpm dmpm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 6 dmpm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 9 m 50 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old November 3rd, 2009, 09:02 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 942 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 10 h 38 m 7 sec
Reputation Power: 5
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++

Reply With Quote
  #3  
Old November 3rd, 2009, 09:26 AM
dmpm dmpm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 6 dmpm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 9 m 50 sec
Reputation Power: 0
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)

Reply With Quote
  #4  
Old November 3rd, 2009, 09:36 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 942 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 10 h 38 m 7 sec
Reputation Power: 5
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:" );

Reply With Quote
  #5  
Old November 3rd, 2009, 11:53 AM
dmpm dmpm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 6 dmpm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 9 m 50 sec
Reputation Power: 0
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..

Reply With Quote
  #6  
Old November 3rd, 2009, 11:57 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 942 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 10 h 38 m 7 sec
Reputation Power: 5
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();

Reply With Quote
  #7  
Old November 3rd, 2009, 12:07 PM
dmpm dmpm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 6 dmpm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 9 m 50 sec
Reputation Power: 0
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.

Reply With Quote
  #8  
Old November 3rd, 2009, 12:58 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 942 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 10 h 38 m 7 sec
Reputation Power: 5
Quick example of what I would more or less do:
cpp Code:
Original - cpp Code
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.    int units;
  8.    float total;
  9.  
  10.    cout << "Please enter units: ";
  11.    cin >> units;
  12.    cin.ignore(); // on linux this is probably not necessary..?
  13.  
  14.    if( cin )
  15.    {
  16.       if( units <= 100 )
  17.       {
  18.          total = units * 5.2f;
  19.       }
  20.       else if( units <= 400 )
  21.       {
  22.          total = 520 + (units - 100) * 3.0f;
  23.       }
  24.       else
  25.       {
  26.          total = 520 + 900 + (units - 400) * 1.4f;
  27.       }
  28.       cout << "\nThe total price is: " << (total / 100.0f) << " Pounds\n";
  29.  
  30.       cout << "Press enter to continue...";
  31.       cout << cin.get();
  32.    }
  33.    // else I/O error
  34.  
  35.    return 0;
  36. }
The problem is that after reading the number there is still a line feed in the buffer. (In windows lines end with carriage return + line feed..)

Reply With Quote
  #9  
Old November 3rd, 2009, 01:25 PM
dmpm dmpm is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2009
Posts: 6 dmpm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 9 m 50 sec
Reputation Power: 0
Quote:
Originally Posted by Icon
Quick example of what I would more or less do:
cpp Code:
Original - cpp Code
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.    int units;
  8.    float total;
  9.  
  10.    cout << "Please enter units: ";
  11.    cin >> units;
  12.    cin.ignore(); // on linux this is probably not necessary..?
  13.  
  14.    if( cin )
  15.    {
  16.       if( units <= 100 )
  17.       {
  18.          total = units * 5.2f;
  19.       }
  20.       else if( units <= 400 )
  21.       {
  22.          total = 520 + (units - 100) * 3.0f;
  23.       }
  24.       else
  25.       {
  26.          total = 520 + 900 + (units - 400) * 1.4f;
  27.       }
  28.       cout << "\nThe total price is: " << (total / 100.0f) << " Pounds\n";
  29.  
  30.       cout << "Press enter to continue...";
  31.       cout << cin.get();
  32.    }
  33.    // else I/O error
  34.  
  35.    return 0;
  36. }
The problem is that after reading the number there is still a line feed in the buffer. (In windows lines end with carriage return + line feed..)


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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Syntax errors - Single app errors?!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek