In: Computer Science
I get the following errors when I type the code
#include <iostream> #include <string.h> #include <bitset> #include <math.h> #define IS_INTEGRAL(T) typename std::enable_if< std::is_integral<T>::value >::type* = 0 using namespace std; template<class T> std::string inttobits1(T byte, IS_INTEGRAL(T)) { std::bitset<sizeof(T)* CHAR_BIT> bs(byte); return bs.to_string(); } int bitstoint1(string bits) { int value; for (int x = 0, y = bits.length() - 1;x < 8 && x < bits.length();x++) { int val = atoi(bits.substr(y, 1).c_str()); value += (int)val * pow(2, x); } return value; } void getlikeyvoltages1(int pinvalue) { //voltages used by OUSB range 0 to 5 int probability = (int)pinvalue / 255;//this is the likely voltage that can be generated when it's value is pinvalue cout << "Votage produced by OUSB when the voltage is " << pinvalue << " = " << probability << endl; } void readpotentiometer1() { unsigned char byte = 0x08;//mybyte string bits = inttobits1(byte); cout << "bits: " << bits << endl;//the bits read getlikeyvoltages1(bitstoint1(bits)); } void readousb1() { FILE* fpipe; char command[] = "ousb io PINC"; // Read the switch state. char line[256]; if (!(fpipe = (FILE*)popen(command, "r"))) { // error if fpipe returns NULL perror("Problems with pipe"); exit(1); } while (fgets(line, sizeof line, fpipe)) { //divide by 4 int val = atoi(line); val = val >> 2;//divide by 4 printf("%d\n", val); //the current value } pclose(fpipe); } int main(int argc, char** argv) { while (1) { readousb1(); string s; cin >> s; //pause the program } return 0; }
Error (active) E0020 identifier "pclose" is
undefined LabPrac2020
Error (active) E0020 identifier "popen" is
undefined LabPrac2020
Warning C6001 Using uninitialized memory
'value'. LabPrac2020
Warning C4018 '<': signed/unsigned
mismatch LabPrac2020
Warning C4244 '+=': conversion from
'double' to 'int', possible loss of data
Error C3861 'popen': identifier not
found
Error C3861 'pclose': identifier not found
it seem the eroor is because you have not included the apropriate header files .
Just include this header file and the code will run.
#include<bits/stdc++.h>
#include <iostream>
#include <string.h>
#include <bitset>
#include <math.h>
//include this header file
#include<bits/stdc++.h>
#define IS_INTEGRAL(T) typename std::enable_if< std::is_integral<T>::value >::type* = 0
using namespace std;
template<class T>
std::string inttobits1(T byte, IS_INTEGRAL(T))
{
std::bitset<sizeof(T)* CHAR_BIT> bs(byte);
return bs.to_string();
}
int bitstoint1(string bits)
{
int value;
for (int x = 0, y = bits.length() - 1;x < 8 && x < bits.length();x++)
{
int val = atoi(bits.substr(y, 1).c_str());
value += (int)val * pow(2, x);
}
return value;
}
void getlikeyvoltages1(int pinvalue)
{
//voltages used by OUSB range 0 to 5
int probability = (int)pinvalue / 255;//this is the likely voltage that can be generated when it's value is pinvalue
cout << "Votage produced by OUSB when the voltage is " << pinvalue << " = " << probability << endl;
}
void readpotentiometer1()
{
unsigned char byte = 0x08;//mybyte
string bits = inttobits1(byte);
cout << "bits: " << bits << endl;//the bits read
getlikeyvoltages1(bitstoint1(bits));
}
void readousb1()
{
FILE* fpipe; char command[] = "ousb io PINC"; // Read the switch state.
char line[256];
if (!(fpipe = (FILE*)popen(command, "r"))) {
// error if fpipe returns NULL
perror("Problems with pipe");
exit(1);
}
while (fgets(line, sizeof line, fpipe)) {
//divide by 4
int val = atoi(line);
val = val >> 2;//divide by 4
printf("%d\n", val); //the current value
}
pclose(fpipe);
}
int main(int argc, char** argv) {
while (1)
{
readousb1();
string s;
cin >> s; //pause the program
}
return 0;
}