|
 |
|
 |
03-22-2004, 12:43 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Mar 2004
Posts: 3
|
C++/C conversion - Not going well
I'm new at programming in both C and C++. I have a project that I am able to compile and run in C++, and am having a bit of trouble in geting it to work for me in C. So far, my curent code is giving me quite a few errors, and when the code does run, I get an access violation. Can anyone help me with what is wrong?
Thank you in advance, for your time in answering a newbie.
Code:
/*//:: 'Virtual' cell phone written in C to calculate cell phone usage,
//:: Display cost per minute over the fake phones service plan,
//:: Displays the amount of service plan time used in Peak minutes and Off
//::peak minutes.
//:: As more calls are made, the screen for the virtual phone will refresh
//::to display
//:: the changes made in minutes and costs.
//:://////////////////////////////////////////////
*/
#include <stdio.h>
int main()
{
double Fee;
float Cost;
int Length, Off, Peak;
char N, O, P, Y, Ans1, Ans2;
Cost = 0.00;
Fee = .39;
Length = 0;
Off = 0;
Peak = 0;
while(Ans1='Y')
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf(" FFFFF A U U X X
\r F A A U U X X
\r FFFF A A U U X
\r F A A A A U U X X
\r F A A UUUUU X X
\r FFFF OOO NN N EEEE
\r F O O N N N E
\r FFF O O N N N EEEE
\r F O O N NN E
\r F OOO N N EEEE ");
printf("\n\nCost: $%d Peak: %d Off: %d", Cost, Peak, Off);
printf("\n\nWould you like to make a call?[Y/N] ");
scanf("%s", Ans1);
if(Ans1 == 'Y' || Ans1 == 'y'){
printf("\nHow many minutes is the call?");
scanf("%d", Length);
printf("\nIs that Peak or Off minutes?[P/O]");
scanf("%s", Ans2);
Ans2 = toupper(Ans2);
if(Ans2 == 'P'){
Peak = Peak + Length;
}else{
Off = Off + Length;
}
if(Peak > 30){
Cost += (Peak - 30) * Fee;
}
if(Off > 200){
Cost += (Off - 200) * Fee;
}
}
}
}
|
|
|
03-22-2004, 02:36 PM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
[color=dark red] Code follows after 8 remarks below.[/color]
1) I removed your drawing. It is buggy. I skipped C and jumped from Pascal to C++, so I am not good with "printf()". But a better approach in general is to make a 2 dimensional array (lets say 40x40 for now) and develop your console drawing on paper in that 40x40 array and print it with a "for" loop in a "for" loop.
2) I removed some annoyances for my self (comments). Sorry about that.
3) Added <ctype.h> for "toupper()".
4) Outcommented "printf("\n\nCost: $%d Peak: %d Off: %d", Cost, Peak, Off);". Added "cout <<..." and <iostream.h> to demonstrate the original buggy line of code. The "cout" version works correctly.
5) Outcommented "N, O, P, Y" to avoid warning: unreferenced local variable.
6) Check "old fashioned" conversion method in the Peak/Off calculation.
7) Input stored in a reference variable instead of value (check for "&" symbols in "Length" and "Ans2". Avoiding run-time errors this way.
8) Hint: Why a double precision? Cents go to two decimals anyway.
Code:
/*
Virtual cell phone written in C to calculate cell phone usage,
Display cost per minute over the fake phones service plan,
Displays the amount of service plan time used in Peak minutes and Off
peak minutes.
As more calls are made, the screen for the virtual phone will refresh
to display
the changes made in minutes and costs.
*/
#include <stdio.h>
#include<ctype.h>
#include <iostream.h>
int main()
{
double Fee;
float Cost;
int Length, Off, Peak;
char Ans1, Ans2; // ,N, O, P, Y,
Cost = 0.00;
Fee = 0.39;
Length = 0;
Off = 0;
Peak = 0;
while(Ans1='Y')
{
printf(" \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
//printf("\n\nCost: $%d Peak: %d Off: %d", Cost, Peak, Off);
cout<<"Cost: $"<<Cost<<" "<<"Peak: "<<Peak<<" "<<"Off: "<<Off<<endl;
printf("\n\nWould you like to make a call?[Y/N] ");
scanf("%s", &Ans1);
if(Ans1 == 'Y' || Ans1 == 'y')
{
printf("\nHow many minutes is the call?");
scanf("%d", &Length);
printf("\nIs that Peak or Off minutes?[P/O]");
scanf("%s", &Ans2);
Ans2 = toupper(Ans2);
if(Ans2 == 'P')
Peak = Peak + Length;
else
Off = Off + Length;
if(Peak > 30)
Cost += (float)((Peak - 30) * Fee);
if(Off > 200)
Cost += (float)((Off - 200) * Fee);
}
}
return 0;
}
__________________
|
|
|
03-22-2004, 02:58 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Mar 2004
Posts: 3
|
The code that I posted above is actually for a programming class I am in. We had to do an assignment that was the same project, but one version written in C, and the other in C++. I have the C++ version working fine, but I am not able to get my C version working (as I'm sure you saw).
The ASCII art was part of a 'splash screen' that was requested. In my C++ version, I do not have problems with the drawings, yet it seems to error in the C version.
Currently, I am using Quincy for the compiler.
As for the double percision.. umm.. unknowing error on my part?
|
|
|
03-22-2004, 05:05 PM
|
#4 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
i think you better get this c++ code running before you try to convert it to c. on my system, it crashes once i enter any response to "Would you like to make a call?[Y/N]".
your compiler is either really smart or really dumb (probably the latter) and is letting you get away with things that shouldn't work.
first and foremost:
i assume you mean the code below (or else your loop never terminates). you will have to make other corrections to make this work right.
you are using scanf incorrectly. you are trying to read a string ("%s") into a char variable (Ans1). this will probably lead to a segment violation (crash), although there are occasions where it will work (it's writing into some memory area that you probably didn't mean to write into). you are also passing it chars instead of char pointers (as well as int pointers).
valmont is right, you are missing "#include <ctype.h>", at least on standard implementations of c/c++.
after these are fixed, it looks to me like your code is already c-compliant and should compile as is.
|
|
|
03-22-2004, 07:13 PM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Not sure Joe, "while(Ans1 == 'Y')" -besides that he needs to initialize "Ans1" first with 'Y' - if he does make these changes, "cout<<"Cost: $"<<Cost<<" "<<"Peak: "<<Peak<<" "<<"Off: "<<Off<<endl;" would never be visible. The whole setup is wrong, but he needs to figure out what will do. I can't since I don't know what exactly is to be achieved.
Also...
Code:
printf("\n\nCost: $%d Peak: %d Off: %d", Cost, Peak, Off);
... the first %d should be a %f since Cost is a float, not an int.
So Isioviel, replace it with this code:
Code:
printf("\n\nCost: $%f Peak: %d Off: %d", Cost, Peak, Off);
Then another thing left: to make the asci drawing: store the drawing in a 2-dimensional array. Makes the code look neat. or else your code looks like this (yes, start copy and pasting again):
Code:
printf(" FFFFF A U U X X"
"\n F A A U U X X"
"\n FFFF A A U U X"
"\n F A A A A U U X X"
"\n F A A UUUUU X X"
"\n\n FFFF OOO NN N EEEE"
"\n F O O N N N E"
"\n FFF O O N N N EEEE"
"\n F O O N NN E"
"\n F OOO N N EEEE" );
Wich is very ugly. Also look at \n. Not \r. Also observe \n\n for the double newline.
Then, did you notice that I can make free calls in Peak and Off? Perhaps this is correct, I can't know.
Also, did you notice I added a "return 0;"? Your main() is an int, so return an int. Most compilers won't give you an error but a warning instead. Some compilers don't even give a warning and convert your int main() to a void main(). Surely we don't want that.
Second last point. Replace your while by:
Because it is exactly the same as your Just a loop that never finishes. you gotta be more clear about your intentions or try to solve it yourself (it is about the last "challenge" left).
Last point:
A bill like $0.3875964 is not realistic for normal usage (in macro econimics it can be). Better is $0.39 or something. So observe the final code and spot all the changes.
__________________
|
|
|
03-22-2004, 07:20 PM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Therefore here is your complete, errorless working code in pure C. Can you spot all the changes I made?
Code:
/*
Virtual cell phone written in C to calculate cell phone usage.
Display cost per minute over the virtual phones service plan.
Displays the amount of service plan time used in Peak minutes and Off
peak minutes.
As more calls are made, the screen for the virtual phone will refresh
to display the changes made in minutes and costs.
*/
#include <stdio.h>
#include<ctype.h>
int main()
{
float Fee;
float Cost;
int Length, Off, Peak;
char Ans1, Ans2; // ,N, O, P, Y;
Cost = 0.00f;
Fee = 0.39f;
Length = Off = Peak = 0;
Ans1='Y';
while(true)
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf(" FFFFF A U U X X"
"\n F A A U U X X"
"\n FFFF A A U U X"
"\n F A A A A U U X X"
"\n F A A UUUUU X X"
"\n\n FFFF OOO NN N EEEE"
"\n F O O N N N E"
"\n FFF O O N N N EEEE"
"\n F O O N NN E"
"\n F OOO N N EEEE" );
printf("\n\nCost: $%.2f Peak: %d Off: %d", Cost, Peak, Off);
printf("\n\nWould you like to make a call?[Y/N] ");
scanf("%s", &Ans1);
if(Ans1 == 'Y' || Ans1 == 'y')
{
printf("How many minutes is the call?");
scanf("%d", &Length);
printf("Is that Peak or Off minutes?[P/O]");
scanf("%s", &Ans2);
Ans2 = toupper(Ans2);
if(Ans2 == 'P')
Peak = Peak + Length;
else
Off = Off + Length;
if(Peak > 30)
Cost += ((Peak - 30) * Fee);
if(Off > 200)
Cost += ((Off - 200) * Fee);
}
}
return 0;
}
Good Luck!
__________________
|
|
|
03-22-2004, 07:44 PM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Okidoky, then the last challenge is left, like I suspected: implement a system so I can exit the menu (if I don't wan't to make calls anymore) but in such a way so I can see my total bill at least once.
Use my code as the initial code since it works perfectly until now.
If you can't then show me your revised code so I can see if you tried. Then I will help you further. Unless someone wants to help you without asking you first to try it for yourself.
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 10:51 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|