Help Me Please

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 43
Reputation: hopeolicious is an unknown quantity at this point 
Solved Threads: 0
hopeolicious hopeolicious is offline Offline
Light Poster

Help Me Please

 
0
  #1
Oct 12th, 2004
This is my new program and it is like so killing me I dont understand all those errors I keep getting



// Description: This is program 1 of 3
// Display person name, address, calculated and
// actual cost of car and color of car 3x. 1 using the For Loop, 1
// using While Loop, and 1 using Do While Loop


#include <iostream.h>
#include <string.h>
#include <stdlib.h>


int x;
char st_name;
char st_address;
char st_color;
float f_ccost;
float f_acost;


void getdata();
void processdata();
void putdata();

void main()
{
for(x=0;x<=2;x++)
{
getdata();
processdata();
putdata();
}
}
void getdata()
{
cout << "Please enter your name: ";
cin >> st_name;

cout << "\n\nPlease enter your address: ";
cin >> st_address;

cout << "\n\nPlease enter the amount you paid for your car: ";
cin >> f_acost;

cout << "\n\nPlease enter your car color: ";
cin >> st_color;
}
void processdata()
{
cout << "\n\nCalculated amount of your car is: ";

if(f_acost < 1200)
{
else if(strcmp(st_color,"blue")==0)
{
f_ccost = f_acost * 1.0 +10/100;
cout << f_ccost;
}
else
{
cout << f_acost;
}
else
{
if(f_acost > 1000 && f_acost < 1500)
{
else if(strcmp(st_color,"black")==0)
{
f_ccost = f_acost * 1.0 + 20/100;
cout << f_ccost;
}
else
{
cout << f_acost;
}
else
{
if(f_acost <= 2000)
{
if(strcmp(st_color,"white")==0)
{
f_accost = f_acost - 500.00;
cout << f_ccost;
}
else
{
cout << f_acost;
}
}
}
void putdata()
{
cout << "Person Name: " << st_name;
cout << "\n\nAddress: " << st_address;

cout << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
cout << "\n\nActual Cost of Car: $ " << f_acost;
cout << "\n\nCalculated Cost of Car: $ " << f_ccost;
cout << "\n\nColor of Car: " << st_color;
}



This is what I have so far but I keep getting all kinds of errors
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Help Me Please

 
0
  #2
Oct 12th, 2004
Greetings,

I have compiled your source, and have discovered many syntax errors. Let's overview them, and correct them one step at a time:

#include <iostream.h>
#include <string.h>
#include <stdlib.h>
It appears to me that your code is written in C++. You should avoid using the *.h version as much as possible, because some implementations have bugs in their *.h version. Moreover, some of them support non-standard code that is not portable, and fail to support some of the standard code of the STL.

Furthermore, the *.h version puts everything in the global namespace. The extension-less version is more stable and it's more portable. It also places everything in the std namespace.

It is mainly recommended to use the following syntax:
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
Globals
Next, we run into three major issues:
char st_name;
char st_address;
char st_color;
This will only allow us to hold a single character of data in each of our variables. If we were looking to hold a multi-character string, we would use an array or pointer allocation. In this case, arrays will suit our task. A character array is simply an array of characters.

In this case, we would modify our current source to something that looks favorable to:
char st_name[25];
char st_address[75];
char st_color[25];
main()
Next, we run into our main() function. As a good programmer, I would like to state that using void as a return-type for main() is a bad habit.
Q. Why?
A. Not declaring a function's return value is an error in C++, whether the function is main or not. In C99, the October 1999 revision to Standard C, not declaring a function's return value is also an error (in the previous version of Standard C an implicit int was assumed as the return value if a function was declared/defined without a return value). However the usual requirement of both Standard C++ and Standard C is that main should be declared to return an int. In other words, this is an acceptable form of main:
int main(void) { /* ... */ }
The problem is that the follwoing code declares main to return a void and that's just no good for a strictly conforming program:

implicit int not allowed in C++ or C99
main() { /* ...Whatever... */ }
We can simply change our code to:
int main() {
	// Stuff here

	return 0;
}
getdata()
Using "cin >>" to input into the stream is very dangerous, especially when dealing with a limited space character array. You could overflow the arrays boundaries as "cin >>" does not support buffer overflow protection. In a more safer environment, we can use getline() as a substitute:
cin.getline(st_name, 25);	// 25 max, anything over and it stops
Doing them for the rest of the character arrays will work.

Q. However, how will it work for my floating-type variable?
A. Create another character array, use getline() and write a function to convert a string to float.

This is possible in part by one of the iostream hierarchy of classes: stringstream [sstream]. In our new function we will send our string to our function, and return the floating value. Though, before we accomplish this, we must add a new character array to our list, and use getline() to retreive our data. Where your other global variables are set, add:
char st_price[25];
Once that is done, go inside getdata() and replace "cin >> f_acost" with:
  1. cin.getline(st_price, 25);
This will now store you data in a temporary string. Next we will write a function to convert a string to float using the stringstream:
float stringToFloat(char *s) {
	float f = 0.0f;
	stringstream ss;
	ss << s;
	ss >> f;
	return f;
}
Quite simple. It's C++, so it's short. Send stringstream our data, send out the float type and return our new variable. Of course in order to get the float variable, we would call on something like:
  1. f_acost = stringToFloat(st_price);
After we call getline() on st_price.

processdata()
Lastly, your function processdata(). There are alot syntatical errors in this, where if/else statements don't match up accordingly, etc... For example:
if(f_acost < 1200) {
	else if(strcmp(st_color,"blue")==0)	{
		f_ccost = f_acost * 1.0 +10/100;
		cout << f_ccost;
	}else {
		cout << f_acost;
	}
else {
Ok, lets stop here. The first if is fine, but look after you open it, you called on an else. The else only acts upon failure of the preceding if:
if (expression)
	statement 1
else	// This else belongs to the if above
	statement 2
This should make sense, so in essence your if/else block should look like this:
if(f_acost < 1200) { // First if opens
	if(strcmp(st_color,"blue")==0) { // Second if opens
		f_ccost = f_acost * 1.0 +10/100;
		cout << f_ccost;
	}else { // else to second if
		cout << f_acost;
	} // Second if closes
} // First if closes
And so on... Also, when you do a strcmp() check for "white"; your variable shows f_accost instead of f_acost, possibly creating another error. Do a check throughout all of your f_ variables, and make sure the names match accordingly.

Conclusion
I hope this does help. If you have further questions, please feel free to ask.


- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC