?
Solved

how can I re-write a C++ program to C?

Posted on 2003-03-18
5
Medium Priority
?
203 Views
Last Modified: 2010-04-01
I am working in a project that involves software code controlled by the Motorola MC68HC11 microcontroller. I wrote the program code in Visual C++, but the only compiler available (at least that i could find) for motorola chips compiles C files and no C++. I am stuck with this problem. This is the code i have in C++ so far:

#include<iostream.h>
#include<iomanip.h>

//****************************************************************
//This is the controller program a robotic arm that is going to be
//controlled by the user's hand.
//****************************************************************

//global variables

const int MAX=100,BEGIN=0;
int enable;


//class definitions

class finger {
public:
     finger();
     float detect_signal(int);
     void move_finger(float,float,int);
     float previous,actual,signal;
     float pres_sensor,servo;
};

//class functions definitions

finger::finger(){
     previous=actual=signal=pres_sensor=BEGIN;
}


float finger::detect_signal(int i){
     float signal;
     cout<<"Enter the signal "<<i<<" value: ";
     cin>>signal;
     return (signal);
}

void finger::move_finger(float actual,float prev,int i){
     actual=actual-prev;
     if (actual<0)
          cout<<"Finger "<<i<<" is moving "<<actual<<" inches up"<<endl;
     else if(actual>0)
          cout<<"Finger "<<i<<" is moving "<<actual<<" inches down"<<endl;
     else if(actual==0)
          cout<<"Finger "<<i<<" is not moving"<<endl;
}



// beginning of the program

int main()

{
     int i;
     finger f[7];
     cout<<"Press 1 if the system is on or 0 if off: ";
     cin>>enable;
     while(enable)
     {
          for(i=1;i<=3;i++)
          {
               cout<<"Enter the pressure sensor "<<i<<" value: ";
               cin>>f[i].pres_sensor;
               cout<<endl;
          }
          if ((f[1].pres_sensor<=MAX)&&(f[2].pres_sensor<=MAX)&&(f[3].pres_sensor<=MAX))
          {
               for(i=1;i<7;i++)
               {    
                    f[i].actual=f[i].detect_signal(i);
                    f[i].move_finger(f[i].actual,f[i].previous,i);
                    f[i].previous=f[i].actual;
               }
               
          }
          if (f[1].pres_sensor>MAX)          //Assumming that pressure sensor 1 is in finger 1
               f[1].pres_sensor=MAX;
          if (f[2].pres_sensor>MAX)          //Assumming that pressure sensor 2 is in finger 2
               f[2].pres_sensor=MAX;
          if (f[3].pres_sensor>MAX)          //Assumming that pressure sensor 3 is in finger 3
               f[3].pres_sensor=MAX;
          for(i=1;i<=3;i++)
               cout<<"The pressure sensor "<<i<<" value is: "<<f[i].pres_sensor<<endl;
          for(i=1;i<=6;i++)
               cout<<"The finger "<<i<<" position is: "<<f[i].actual<<endl;
          cout<<endl<<"Check enable (1/0): ";
          cin>>enable;
     }
     cout<<endl<<"Moving to beginning state..."<<endl;
     for (i=1;i<=6;i++)
     {
          f[i].actual=BEGIN-f[i].previous;
          cout<<"The finger "<<i<<" is moving "<<f[i].actual<<" inches to the beginning state"<<endl;
     }
     return 0;
}



I don't know what to do, i'm really stuck...I hope i can get an answer soon...Thank you!!!

Miguel Torres
Devry University Chicago, IL
0
Comment
Question by:migueltorres
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Accepted Solution

by:
YuriPutivsky earned 400 total points
ID: 8163069
Hi migueltorres,
I could be like this...

#include<iostream.h>
#include<iomanip.h>

//****************************************************************
//This is the controller program a robotic arm that is going to be
//controlled by the user's hand.
//****************************************************************

//global variables

const int MAX=100,BEGIN=0;
int enable;


//struct definitions

struct finger {
    float previous;
    float actual;
    float signal;
    float pres_sensor
    float servo;
};

void init_finger(finger* src)
{
   src->previous = src->actual = src->signal = src->pres_sensor = BEGIN;
}

float detect_signal(int i){
    float signal;
    cout<<"Enter the signal "<<i<<" value: ";
    cin>>signal;
    return signal;
}

void move_finger(finger* src, float act, float prev, int i)
{
    src->actual = act-prev;
    if (src->actual<0)
         cout<<"Finger "<<i<<" is moving "<<actual<<" inches up"<<endl;
    else if(src->actual>0)
         cout<<"Finger "<<i<<" is moving "<<actual<<" inches down"<<endl;
    else if(src->actual==0)
         cout<<"Finger "<<i<<" is not moving"<<endl;
}



// beginning of the program

int main()

{
    int i;
    finger f[7];
    for(i=1;i<7;i++)
       init_finger(&f[i]);


    cout<<"Press 1 if the system is on or 0 if off: ";
    cin>>enable;
    while(enable)
    {
         for(i=1;i<=3;i++)
         {
              cout<<"Enter the pressure sensor "<<i<<" value: ";
              cin>>f[i].pres_sensor;
              cout<<endl;
         }
         if ((f[1].pres_sensor<=MAX)&&(f[2].pres_sensor<=MAX)&&(f[3].pres_sensor<=MAX))
         {
              for(i=1;i<7;i++)
              {    
                   f[i].actual=detect_signal(i);
                   move_finger(&f[i], f[i].actual, f[i].previous, i);
                   f[i].previous = f[i].actual;
              }
             
         }
         if (f[1].pres_sensor>MAX)          //Assumming that pressure sensor 1 is in finger 1
              f[1].pres_sensor=MAX;
         if (f[2].pres_sensor>MAX)          //Assumming that pressure sensor 2 is in finger 2
              f[2].pres_sensor=MAX;
         if (f[3].pres_sensor>MAX)          //Assumming that pressure sensor 3 is in finger 3
              f[3].pres_sensor=MAX;
         for(i=1;i<=3;i++)
              cout<<"The pressure sensor "<<i<<" value is: "<<f[i].pres_sensor<<endl;
         for(i=1;i<=6;i++)
              cout<<"The finger "<<i<<" position is: "<<f[i].actual<<endl;
         cout<<endl<<"Check enable (1/0): ";
         cin>>enable;
    }
    cout<<endl<<"Moving to beginning state..."<<endl;
    for (i=1;i<=6;i++)
    {
         f[i].actual=BEGIN-f[i].previous;
         cout<<"The finger "<<i<<" is moving "<<f[i].actual<<" inches to the beginning state"<<endl;
    }
    return 0;
}

0
 
LVL 5

Expert Comment

by:Kocil
ID: 8163785
You are very right Yuri, except you forgot that
cout << "something"
is definitely C++.

Use the old printf :)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8164555
<iostream> too: -> switch to stdio

[Also replace all the cins by scanfs.]

Mayank.
0
 
LVL 12

Expert Comment

by:Salte
ID: 8165611
I will suggest you do not use scanf.

Use fgets() and then analyze the line just input manually.


Example, you want to input a decimal integer, a double and a string of maximum 5 chars and terminated by any non-alphanumeric character and a string of maximum 7 chars terminated by '.' and a string of exactly 4 chars.

There may be blanks between each of these items.

char buf[100];

fgets(buf,sizeof(buf),stdin);

char * p;
char * q;
int k, i;
double x;
char s1[6];
char s2[8];
char s3[5];

// first the int.

k = strtol(buf, & p, 10);
// then the float.
x = strtod(p, & q);
// then skip blanks.
while (isblank(*q)) ++q;
// then up to 5 characters but stop at any non-alphanumeric.

for (i = 0; i < 5 && isalnum(q[i]); ++i);
memcpy(s1,q,i);
s1[i] = '\0';

// skip blanks again.
q += i;
while (isblank(*q)) ++q;
// up to 7 characters but stop at '.'.
p = strchr(q,'.');
if (p == 0 || p > q + 7)
   p = q + 7;
i = p - q;
memcpy(s2,q,i);
s2[i] = '\0';

// skip blanks again
while (isblank(*p)) ++p;
// exactly 4 characters.
memcpy(s3,p,4);
s3[4] = '\0';

// ignore the rest or complain if it is not just blanks followed by \n.

This is better than scanf because you can easily control what is the exact condition for stopping a string, to give a reasonable error message if the input format isn't what you expect and check for the format to be correct while you parse the input string.

Alf
0
 

Author Comment

by:migueltorres
ID: 8169713
thank you!!! i can take it from there
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question