•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,774 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,426 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1344 | Replies: 1
![]() |
•
•
Join Date: Oct 2004
Posts: 43
Reputation:
Rep Power: 4
Solved Threads: 0
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
// 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
•
•
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation:
Rep Power: 4
Solved Threads: 4
Greetings,
I have compiled your source, and have discovered many syntax errors. Let's overview them, and correct them one step at a time:
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:
Globals
Next, we run into three major issues: 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:
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: 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 We can simply change our code to:
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:
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: 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: 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: 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: 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: This should make sense, so in essence your if/else block should look like this: 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
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>
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;
Next, we run into three major issues:
char st_name; char st_address; char st_color;
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];
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) { /* ... */ }
implicit int not allowed in C++ or C99
main() { /* ...Whatever... */ }int main() { // Stuff here return 0; }
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 stopsQ. 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];cin.getline(st_price, 25);
float stringToFloat(char *s) { float f = 0.0f; stringstream ss; ss << s; ss >> f; return f; }
f_acost = stringToFloat(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 {
if (expression) statement 1 else // This else belongs to the if above statement 2
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
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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Other Threads in the C++ Forum
- Previous Thread: compiler
- Next Thread: program overwritng the 1st input


Linear Mode