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:
Once that is done, go inside getdata() and replace "cin >> f_acost" with:
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:
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