hi i am kind of new to programming, i have managed to create a simple payroll database in C++, but now i am trying to convert in into C but i am keep getting error, can anyone help me convert this code to C. I really need this so thatr i can learn my mistakes in C. Below is the C++ code that i have have developed.
Thank you, 14 2448 sicarie 4,677
Recognized Expert Moderator Specialist
Well, you're going to have a few issues, but those will mostly deal with I/O. I'd recommend going through and replacing all the couts first. All your control structures will be the same, so I'm pretty sure you'll just need to do the cin/couts and the file IO. There might be a few other things that I'm missing on the spot-check, but those should get you down to a level where the errors in compilation will show you any of the others.
hi i tried to change the cout function to printf it can work perfectly, but when i use any stdin function's like scanf, i keep getting errors. I am also having trouble changing the stream functions. i have tried online tutorial's for the past 1 week but i am stuck in the scanf, and stream file functions. Please help me. Thank you
sicarie 4,677
Recognized Expert Moderator Specialist
hi i tried to change the cout function to printf it can work perfectly, but when i use any stdin function's like scanf, i keep getting errors. I am also having trouble changing the stream functions. i have tried online tutorial's for the past 1 week but i am stuck in the scanf, and stream file functions. Please help me. Thank you
Have you changed the libraries that handle those things? C and C++ use different headers...
Have you changed the libraries that handle those things? C and C++ use different headers...
I have changed the the header file to <stdio.h>, actually now my scanf function is working but i am not sure how to change some part of the coding to C language especially the file processing section. Below is my coding that i have have modify. Can you help in how to change the file processing/database part.
I am also having trouble with the switch case, after i have changed it to printf statement the case"2" is not working, but i think this is because i have not yet finish changing the whole coding. Am I right about this or is there anything when wrong when i convert the switch statement?
Thank you for your help
[ - #include<fstream>
-
#include<string>
-
#include<iostream>
-
#include<iomanip>
-
#include<stdio.h>
-
using namespace std;
-
struct employee{ string fname, lname;
-
double hourlyrate, hoursworked, netpay;
-
}emp; //employee
-
employee e[100];
-
int n=0;
-
void load(){
-
ifstream fin("employee.txt",ios::in);
-
while(fin>>e[n].fname>>e[n].lname>>e[n].hourlyrate>>e[n].hoursworked){
-
e[n].netpay=e[n].hourlyrate*e[n].hoursworked;
-
n++;}//WHILE
-
}//LOAD
-
void store(){
-
ofstream fout("employee.txt",ios::out);
-
for(int i=0; i<n; i++){
-
if(e[i].fname!=" ")
-
fout<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].hourlyrate<<" "<<e[i].hoursworked<<endl;
-
}//FOR
-
}//STORE
-
-
void insert(){
-
printf("Enter employee's first name: ");
-
scanf("s",& emp.fname);
-
printf("Enter employee's last name: ");
-
scanf("s",& emp.lname);
-
cout<<"Enter employee's salary per hour: "; cin>>e[n].hourlyrate;
-
if(cin.fail())
-
{cout<<"Error\n"; exit(1);}
-
cout<<"Enter employee's hours worked: ";
-
cin>>e[n].hoursworked;
-
e[n].netpay=e[n].hourlyrate*e[n].hoursworked;
-
n++; }//INSERTS THE RECORD TO ARRAY
-
-
void display(){
-
-
cout<<setiosflags(ios::left)<<setw(15)<<"First Name"<<setw(15)<<"Last Name";
-
cout<<setw(10)<<"Hourly Rate"<<setw(10)<<"HrsWorked"<<setw(10)<<"Net Pay";
-
cout<<endl<<endl;
-
cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);
-
for(int i=0;i<n;i++){
-
if(e[i].fname!=" "){
-
cout<<setiosflags(ios::left)<<setw(15)<<e[i].fname<<setw(15)<<e[i].lname;
-
cout<<setprecision(2)<<setiosflags(ios::left)<<setw(10)<<e[i].hourlyrate;
-
cout<<setw(10)<<e[i].hoursworked<<setw(10)<<e[i].netpay;
-
cout<<resetiosflags(ios::left)<<endl;}//IF
-
}//FOR
-
}//THIS FUNCTION DISPLAYS ALL RECORDS IN ARRAY
-
-
int search(string s){
-
for(int i=0;i<n;i++)if(s==e[i].lname)return i;//FOR
-
return -1;
-
}//SEARCH BY NAME,-1 NOT FOUND
-
void update(){
-
string searchn;
-
cout<<"Enter the Last Name of the Employee to be modified: "; cin>>searchn;
-
int i=search(searchn);
-
if(i==-1) cout<<"EMPLOYEE NOT LISTED"<<endl;
-
else{
-
cout<<endl<<"Please enter the updated information."<<endl;
-
cout<<"Enter employee's first name: "; cin>>e[i].fname;
-
cout<<"Enter employee's last name: "; cin>>e[i].lname;
-
cout<<"Enter employee's rate per hour: "; cin>>e[i].hourlyrate;
-
cout<<"Enter employee's hours worked: "; cin>>e[i].hoursworked;
-
e[i].netpay=(e[i].hourlyrate*e[i].hoursworked);}
-
}//UPDATE
-
-
void deleterec(){
-
string searchn;
-
cout<<"Enter the Last Name of the Employee to delete: "; cin>>searchn;
-
int k=-1;
-
for(int j=0; j<n;j++){
-
if(searchn==e[j].lname)
-
k=j;}
-
if(k!=-1)
-
{ cout<<" Name: "<<e[k].fname<<" "<<e[k].lname<<endl;
-
cout<<" Salary: "<<e[k].hourlyrate<<endl;
-
cout<<" Hrs Worked: "<<e[k].hoursworked<<endl;
-
cout<<" Net Pay: "<<e[k].netpay<<endl<<endl;}
-
-
char ans;
-
cout<<endl<<"Do you want to delete this record? (Y/N): ";
-
cin>>ans;
-
if(ans=='Y'||ans=='y'){
-
e[k].fname=" ";}//IF
-
else cout<<"EMPLOYEE NOT FOUND"; }//DELETE
-
-
void report(){
-
int i=0;
-
double totalhrs=0.00, totalpay=0.00;
-
for(i=0;i<n;i++){
-
totalhrs+=e[i].hoursworked;
-
totalpay+=e[i].netpay; }//FOR
-
cout<<"Total number of employees: " <<n<<endl<<endl;
-
cout<<"Total hours worked by all employees: "<<totalhrs<<endl<<endl;
-
cout<<"Total pay of all employees: "<<totalpay<<endl<<endl; }//REPORT
-
-
void deletefile(){
-
int pword;
-
cout<<"Enter your administrative password: "; cin>>pword;
-
if(pword==5438){
-
ofstream record("employee.txt", ios::out);
-
record<<endl;
-
load();
-
record.close();}
-
else cout<<"Wrong password entered."<<endl; }//DELETE FILE
-
- void backupfile(){
-
system("copy employee.txt backup.txt");
-
cout<<endl<<"Back-up file has been made."<<endl;
-
}
-
-
void main(){
-
load();
-
char choice='0';
-
while (choice!='e'&&choice!='E'){
-
printf("\nPayroll Database\n\n");
-
printf("1. Insert Employee Record and Compute Net Pay\n");
-
printf("2. Display All Records\n");
-
printf("3. Search Database\n");
-
printf("4. Update Record\n");
-
printf("5. Delete Record\n");
-
printf("6. Generate Report\n");
-
printf("7. Delete Database File\n");
-
printf("8. Back-up Database File\n");
-
printf("\nType e to exit\n\n");
-
printf("Enter the number of your choice:\n");
-
scanf("%c",&choice);
-
switch (choice){
-
-
case '1': insert(); break;
-
case '2': display(); break;
-
case '3': char s[100]; int index;
-
cout<<"Enter the last name of the person you want: ";
-
cin>>s;
-
index=search(s);
-
if(index!=-1) {
-
cout<<"Name: "<<e[index].fname<<" "<<e[index].lname<<endl;
-
cout<<"Hourly Rate: "<<e[index].hourlyrate<<endl;
-
cout<<"Hrs Worked: "<<e[index].hoursworked<<endl;
-
cout<<"Net Pay: "<<e[index].netpay<<endl<<endl;}//IF
-
else cout<<"EMPLOYEE NOT FOUND"<<endl;
-
break;
-
case '4': update(); break;
-
case '5': deleterec(); break;
-
case '6': report(); break;
-
case '7': deletefile(); break;
-
case '8': backupfile(); break;
-
case 'e': store(); break; //EXIT
-
default: cout<<"Invalid Choice"<<endl; break; }//SWITCH
-
}//WHILE
-
}//MAIN
-
The bold section's are the part i am having problem to convert to C language. Sorry for the trouble.
RRick 463
Recognized Expert Contributor
There's another way to convert the C++ code to C and you don't have to get rid of the C++ objects. You do this by using the extern C declaration. This will allow the function signature to look like C, and be called like a C function, but the function internals are still C++. Try this link http://www.parashift.com/c++-faq-lit....html#faq-32.1 and refer to section 32.6.
There are a couple of caveats - All the function parameters must be C style variables. No C++ objects (like string) are permitted.
- When you link your C program with these routines, you will have to explictly add the C++ libraries. One way around this problem is to use the C++ compiler to compile your C program. The C++ compiler knows where the C and C++ libraries reside and the C++ compiler is a "better" C compiler.
I am sorry, i am trying to convert this c++ coding to c without mixing both the language, please help with some idea in the bolded section's in my coding.
RRick 463
Recognized Expert Contributor
Most of your issues are with converting C++ print and file mechanisms to C.
Instead of opening a file with ofstream use fopen. Its parameters are almost the same as ofstream. Use "r" and "w" instead ios::in and out.
C style printf and fprintf are going to become very good friends. Their format is different from <<, but they have very good formating capabilities. Instead of setw, you can use "%15s" for printing a string with a width of 15, and "%-15s" reverses the justification. A google search will find the various options for (f)printf.
Can anyone give show me a example how to use my fopen in this part of my coding: - fout<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].hourlyrate<<" "<<e[i].hoursworked<<endl;
How can i this part i am confused with the "::" parts: - cout<<setiosflags(ios::left)<<setw(15)<<"First Name"<<setw(15)<<"Last Name";
-
cout<<setw(10)<<"Hourly Rate"<<setw(10)<<"HrsWorked"<<setw(10)<<"Net Pay";
-
cout<<endl<<endl;
-
cout<<setiosflags(ios::fixed|ios::showpoint|ios::left);
-
for(int i=0;i<n;i++){
-
if(e[i].fname!=" "){
-
cout<<setiosflags(ios::left)<<setw(15)<<e[i].fname<<setw(15)<<e[i].lname;
-
cout<<setprecision(2)<<setiosflags(ios::left)<<setw(10)<<e[i].hourlyrate;
-
cout<<setw(10)<<e[i].hoursworked<<setw(10)<<e[i].netpay;
-
cout<<resetiosflags(ios::left)<<endl;}//IF
-
}//FOR
RRick 463
Recognized Expert Contributor
fout is used to print something, so you can't use fopen at that part of the program. Fopen is only used to "open" files, not print them.
Instead, you use fprintf. Your fout printing would translate into C as: -
// C++ print
-
fout<<e[i].fname<<" "<<e[i].lname<<" "<<e[i].hourlyrate<<" "<<e[i].hoursworked<<endl;
-
-
/* C print */
-
FILE * fhandle = fopen( ....);
-
fprintf( fhandle, "%s %s %d %d\n", e[i].fname, e[i].lname, e[i].hourlyrate, e[i].hoursworked);
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Newbee Adam |
last post by:
I have been reading in help how I need to use decimal becuase currency does
not exist like I used in vb6.
I had a difficult time on google and msdn finding how or if I can take the
value of text box as decimal or do I just have to make another decimal
variable
.. So I took a guess hoping CDec would come up blue if I type it in with no
squigglies on the line. But is this actually converting it to decimal.
Next if so, I do not like the...
|
by: Megat |
last post by:
I'm trying to create a conversion program, that convert a simple
proprietry programming language to an international standard
languange, using Visual C++. Need some help from those who has
experience or anyone who has any idea in creating such program.
Really appreciate your help.
Thank you
|
by: prabha |
last post by:
Hello Everybody,
I have to conert the word doc to multiple html files,according to the templates in the word doc.
I had converted the word to xml.Also through Exsl ,had finished the multiple output html files.
The problem is while reading through the worddoc paragraph,the special characters are not identified.
So in the xml file,it's just storing that as "?".So I couldn't able to retrive the characters in my ouput html files.
|
by: Jake Jessup |
last post by:
A client wants me to do a conversion from Access 2.0 (yeah, I know, unreal).
The problem is, she doesn't have the install disks and I haven't had a copy
of 2.0 in years.
If some kind should could kindly point me to a place where I might get a
copy, I would be grateful.
I'm willing to do a trade for it.
|
by: Robert |
last post by:
I have a series of web applications (configured as separate applications) on
a server.
There is a main application at the root and then several virtual directories
that are independant applications.
I am testing an upgrade of all of the sites and have converted the main root
site...although not necessarily fixed any issues.
I move on instead and converted one of the virtual roots that is a seperate
| |
by: sam_cit |
last post by:
Hi,
I needed help in converting a character to the correspoding
hexadecimal values, like the following example,
ASCII value : ABC
Hex Code Value : %41%42%43...
whats the logic of conversion...
|
by: manmit.walia |
last post by:
Hello All,
I have tried multiple online tools to convert an VB6 (bas) file to
VB.NET file and no luck. I was hoping that someone could help me covert
this. I am new to the .NET world and still learning all help would be
greatly apperciated.
Attribute VB_Name = "Module1"
Option Explicit
|
by: Mhel |
last post by:
Hi everyone! I need some help in Converting Infix to postfix.I need a program that would do it.
Please, I really need help.
|
by: Hank Stalica |
last post by:
I'm having this weird problem where my code does the following
conversion from string to float:
27000000.0 -27000000.00
2973999.99 -29740000.00
2989999.13 -2989999.25
The number on the left is the string I get after tokenizing a bigger
string. The number on the right is the number I get after the conversion.
|
by: helpwithcode |
last post by:
Hi people,
I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements,
String string_dob=text_dob.getText();
//Converting string to date
System.out.println(string_dob);
s.Info_DOB=Date.valueOf(string_dob);
runs perfectly fine in the method insert() and throws up an illegal Exception in the method UPDATE.This is the error I get when I pass a date "1979-05-02"
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |