C++/C conversion to just C - Page 3
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 42 of 42

Thread: C++/C conversion to just C

  1. #31
    Join Date
    Aug 2009
    Posts
    439

    Re: C++/C conversion to just C

    Set a break point at
    Code:
     int Finished = 0;
    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 01:38 AM.

  2. #32
    Join Date
    Feb 2012
    Posts
    68

    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?

  3. #33
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,292

    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 LITE 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.

  4. #34
    Join Date
    Aug 2009
    Posts
    439

    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 02:18 AM.

  5. #35
    Join Date
    Feb 2012
    Posts
    68

    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 07:51 AM.

  6. #36
    Join Date
    Mar 2013
    Posts
    3

    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

  7. #37
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,392

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #38
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: C++/C conversion to just C

    Quote Originally Posted by Charmmanuel View Post
    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

  9. #39
    Join Date
    Mar 2013
    Posts
    3

    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++ ..

  10. #40
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: C++/C conversion to just C

    Quote Originally Posted by Charmmanuel View Post
    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

  11. #41
    Join Date
    Mar 2013
    Posts
    3

    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";


    }

  12. #42
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: C++/C conversion to just C

    Quote Originally Posted by Charmmanuel View Post
    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

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center