-
February 20th, 2012, 02:34 AM
#31
Re: C++/C conversion to just C
Set a break point at
and step from there. The problem should show up after a couple of steps. Also...your formatting for the switch statement...seems rather odd to me. I would do something like:
Code:
switch (choice)
{
// accesses the functions
case 0:
Finished = 1;
break;
case 1:
addition(&reward);
break;
case 2:
subtraction(&reward);
break;
default :
printf("Error in input\n\n");
break;
}
Sure, it is more lines of code, but imagine each of the cases requiring 10 instructions. I think the way I have it above would be more clear. But, that is just a personal preference.
Last edited by Alterah; February 20th, 2012 at 02:38 AM.
-
February 20th, 2012, 03:01 AM
#32
Re: C++/C conversion to just C
But in my watches all it says is
Function arguements
Finished = no symbol "finished" in current context
choice = no symbol.... etc.
what is that telling me?? i declared them they are symbolizing something, so it should work right?
-
February 20th, 2012, 03:10 AM
#33
Re: C++/C conversion to just C
At this point, I'd review how you created your "project": Is the file you are editing actually part of the project? Are you sure you are editing the correct "main.c"?
It sounds like your program is compiling, but not the file you are writing. that, or not running the proper objects.
When these errors come up, and especially on small scale projects, I recommend you use "rebuild everything", just to make sure.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
February 20th, 2012, 03:14 AM
#34
Re: C++/C conversion to just C
Consider the following:
Code:
int main()
{
int foo;
bar();
}
void bar()
{
// I have no knowledge of "foo"
}
This is what the message is getting at. Are you able to step through the code at all? I was able to step through the code, fix the error I found with the debugger, and run your code.
One thing you can try is this:
File > New > Project > Console applicate > Run through the wizard, name your project. Then, you will have new project. Just copy and paste your code into the new main.c file of that project. Build it, and try debugging that. Like monarch dodra suggested, your project may be set up incorrectly.
Last edited by Alterah; February 20th, 2012 at 03:18 AM.
-
February 20th, 2012, 08:38 AM
#35
Re: C++/C conversion to just C
ok the problem was just at the while loop. had to get rid of ; and made it == instead of =. fml hahahaha.
well thank you everyone for coming on this journey with me. Conversion to C using pointers success!
On a final note does anyone know how to make the case selection letter selection like A) B) C) instead of 1 2 3 ?
Last edited by chucker; February 20th, 2012 at 08:51 AM.
-
March 21st, 2013, 07:19 AM
#36
Re: C++/C conversion to just C
Hello..good day..can you help me,about this? c++ to c...
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void EmployeeInfo ();
void input();
void printstars ();
void paycompute();
void date_coverage();
//global variables
string E_code,E_name,E_record,E_level,d_coverage;;
double perday,T_Hours,T_Mins;
int main(){
do{
system("cls");
//Accept input from user
cout<<"Enter Employee ID: ";
getline(cin,E_code);
// opens file for input
ifstream inFile("employee.txt",ios::in);
// if input file can not be open enter the fail state
if(!inFile) {
cout << "Can not open input file" << endl;
cout << "The program will terminate" << endl;
system("pause");
exit(0);
}
//search for employee code
do{
getline(inFile,E_record);
int location = E_record.find(E_code);
if(location>0)
{
E_code = E_record.substr(location,8);
E_level = E_record.substr(E_record.length()-1,1);
E_name = E_record.substr(0,location-1);
break;
}
}while(!inFile.eof());
if (E_level =="1") perday = 380;
if (E_level =="2") perday = 450;
if (E_level =="3") perday = 550;
//Display Records
if (E_name==""){
cout<<"Record not Found!\n\n\n";
system("pause");
exit(0);
}
else {
EmployeeInfo ();
input();
date_coverage();
EmployeeInfo ();
paycompute();
cout<<"\n\n";
system("Pause");
}
}while(true);
}//END
//MY USER-DEFINED FUNCTIONS
void EmployeeInfo (){
system("cls");
printstars ();
cout<<"\n\nEmployee Name :\t"<<E_name;
cout<<"\nEmployee Code :\t"<<E_code;
cout<<"\nEmployee Level:\t"<<"Level "<<E_level;
cout<<"\nEmployee Rate :\t"<<"Php"<<perday<<".00/day";
printstars ();
}
void input ()
{
// declare variables --------
int hrs_in[5],hrs_out[5], min_in[5],min_out[5],x,TH_inout[5];
char colon;
string days []={"Monday","Tuesday","Wednesday","Thursday","Friday"};
// accept input -------
cout<<"\n\nEnter the time using the following format(hour:minutes)\n";
for(x=0;x<5;x++){
cout<<"\n\nEnter Log-in for "<<days[x]<<" : ";
cin>>hrs_in[x]>>colon>>min_in[x];
cout<<"Enter Log-out for "<<days[x]<<" : ";
cin>>hrs_out[x]>>colon>>min_out[x];
}
//write data to text files ------------
ofstream out("dtr.txt",ios:ut);
out<<E_code;
for(x=0;x<5;x++){
out<<","<<hrs_in[x]<<":"<<min_in[x]<<","<<hrs_out[x]<<":"<<min_out[x];
} // out<<"\n\nTotal Working Hours: "<<T_Hours<<" Hrs";
cout<<"Files successfully written to dtr.txt files!";
cout<<"\n\n";
system("pause");
//compute number of Total Hours and Minutes --------
for(x=0;x<5;x++){
if(hrs_in[x]<8)
{hrs_in[x]=8;
min_in[x]=00;
}
if(hrs_in[x]>=12 && hrs_in[x]<13)
{
hrs_in[x]=13;
min_in[x]=00;
}
if(hrs_in[x]>=12 && hrs_in[x]<=13)
{TH_inout[x] = (hrs_out[x]-hrs_in[x]);}
else
{TH_inout[x] = (hrs_out[x]-hrs_in[x])-1;}
T_Hours =T_Hours+TH_inout[x];
}
}
void printstars (){
cout<<"\n=================================================";
}
void date_coverage(){
system("cls");
printstars();
cout<<"\nEnter Payroll Date of Coverage: ";
getline(cin,d_coverage);
getline(cin,d_coverage);
}
void paycompute(){
cout<<"\n\nDate of Coverage\t: "<<d_coverage;
cout<<"\nTotal Work Hours\t: "<<T_Hours<<".00 Hrs";
cout<<"\nWeekly Regular Salary\t: "<<"Php"<<((T_Hours/8)*perday)<<".00";
cout<<"\n";
printstars ();
cout<<"\n\n";
}
thanks
-
March 21st, 2013, 07:47 AM
#37
Re: C++/C conversion to just C
Please format your code using [coe] tags as what you have posted is almost unreadable. use Go Advanced, select code then click '#'.
Help you about what? You haven't asked a question! If you want to convert this c++ code to c (why??) then as a starter for 10 you need to replace all references to the c++ classes (cin, cout, ifstream etc) with appropriate code using just the c library functions (printf, fread etc).
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 21st, 2013, 08:16 AM
#38
Re: C++/C conversion to just C
Originally Posted by Charmmanuel
Hello..good day..can you help me,about this? c++ to c...
Did you read the whole thread?
There is a lot of opinions what and how to achieve it. What is the problem?
Victor Nijegorodov
-
March 21st, 2013, 08:18 AM
#39
Re: C++/C conversion to just C
hello.. Im still a student... I need help on that code..to make it c function not c++ ..
-
March 21st, 2013, 08:23 AM
#40
Re: C++/C conversion to just C
Originally Posted by Charmmanuel
hello.. Im still a student... I need help on that code..to make it c function not c++ ..
I am confused...
What happens? "still a student" cannot read? "still a student" has no idea how tocompile C file?
Victor Nijegorodov
-
March 21st, 2013, 08:27 AM
#41
Re: C++/C conversion to just C
I just need someone who could help me about converting this C++ TO C FUNCTION.. this codes:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void EmployeeInfo ();
void input();
void printstars ();
void paycompute();
void date_coverage();
//global variables
string E_code,E_name,E_record,E_level,d_coverage;;
double perday,T_Hours,T_Mins;
int main(){
do{
system("cls");
//Accept input from user
cout<<"Enter Employee ID: ";
getline(cin,E_code);
// opens file for input
ifstream inFile("employee.txt",ios::in);
// if input file can not be open enter the fail state
if(!inFile) {
cout << "Can not open input file" << endl;
cout << "The program will terminate" << endl;
system("pause");
exit(0);
}
//search for employee code
do{
getline(inFile,E_record);
int location = E_record.find(E_code);
if(location>0)
{
E_code = E_record.substr(location,8);
E_level = E_record.substr(E_record.length()-1,1);
E_name = E_record.substr(0,location-1);
break;
}
}while(!inFile.eof());
if (E_level =="1") perday = 380;
if (E_level =="2") perday = 450;
if (E_level =="3") perday = 550;
//Display Records
if (E_name==""){
cout<<"Record not Found!\n\n\n";
system("pause");
exit(0);
}
else {
EmployeeInfo ();
input();
date_coverage();
EmployeeInfo ();
paycompute();
cout<<"\n\n";
system("Pause");
}
}while(true);
}//END
//MY USER-DEFINED FUNCTIONS
void EmployeeInfo (){
system("cls");
printstars ();
cout<<"\n\nEmployee Name :\t"<<E_name;
cout<<"\nEmployee Code :\t"<<E_code;
cout<<"\nEmployee Level:\t"<<"Level "<<E_level;
cout<<"\nEmployee Rate :\t"<<"Php"<<perday<<".00/day";
printstars ();
}
void input ()
{
// declare variables --------
int hrs_in[5],hrs_out[5], min_in[5],min_out[5],x,TH_inout[5];
char colon;
string days []={"Monday","Tuesday","Wednesday","Thursday","Friday"};
// accept input -------
cout<<"\n\nEnter the time using the following format(hour:minutes)\n";
for(x=0;x<5;x++){
cout<<"\n\nEnter Log-in for "<<days[x]<<" : ";
cin>>hrs_in[x]>>colon>>min_in[x];
cout<<"Enter Log-out for "<<days[x]<<" : ";
cin>>hrs_out[x]>>colon>>min_out[x];
}
//write data to text files ------------
ofstream out("dtr.txt",ios:ut);
out<<E_code;
for(x=0;x<5;x++){
out<<","<<hrs_in[x]<<":"<<min_in[x]<<","<<hrs_out[x]<<":"<<min_out[x];
} // out<<"\n\nTotal Working Hours: "<<T_Hours<<" Hrs";
cout<<"Files successfully written to dtr.txt files!";
cout<<"\n\n";
system("pause");
//compute number of Total Hours and Minutes --------
for(x=0;x<5;x++){
if(hrs_in[x]<8)
{hrs_in[x]=8;
min_in[x]=00;
}
if(hrs_in[x]>=12 && hrs_in[x]<13)
{
hrs_in[x]=13;
min_in[x]=00;
}
if(hrs_in[x]>=12 && hrs_in[x]<=13)
{TH_inout[x] = (hrs_out[x]-hrs_in[x]);}
else
{TH_inout[x] = (hrs_out[x]-hrs_in[x])-1;}
T_Hours =T_Hours+TH_inout[x];
}
}
void printstars (){
cout<<"\n=================================================";
}
void date_coverage(){
system("cls");
printstars();
cout<<"\nEnter Payroll Date of Coverage: ";
getline(cin,d_coverage);
getline(cin,d_coverage);
}
void paycompute(){
cout<<"\n\nDate of Coverage\t: "<<d_coverage;
cout<<"\nTotal Work Hours\t: "<<T_Hours<<".00 Hrs";
cout<<"\nWeekly Regular Salary\t: "<<"Php"<<((T_Hours/8)*perday)<<".00";
cout<<"\n";
printstars ();
cout<<"\n\n";
}
-
March 21st, 2013, 08:31 AM
#42
Re: C++/C conversion to just C
Originally Posted by Charmmanuel
I just need someone who could help me about converting this C++ TO C FUNCTION.. this codes:
Please, never post unformatted code snippets! No one will read it because such a snippet is unreadable!
Didn't you read the answers to your first post here? For example, post#37?
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|