When I send a variable into Excel it changes its value. It just happens with excel. It also just happens when the variable is stored in a container. I guess it is more clear if you see the code:
#include<iostream>
#include<array>
#include<vector>
#include<fstream>
const int aSize{ 150000 };
std::array<double, aSize> anArray{};
int main(void)
{
double aValue{ 0.00000005467 };
std::vector<double> aVector;
for (int i = 0; i < aSize; ++i)
{
anArray[i] = aValue;
aVector.push_back(aValue);
}
std::ofstream fileOne, fileTwo, fileThree, fileFour, fileFive;
fileOne.open("array.xls");
fileTwo.open("array.txt");
fileThree.open("vector.xls");
fileFour.open("vector.txt");
fileFive.open("value.xls");
fileOne << anArray[0];
fileTwo << anArray[0];
fileThree << aVector[0];
fileFour << aVector[0];
fileFive << aValue;
std::cout << aValue << "\n" << anArray[0] << "\n" << aVector[0];
return 0;
}
All I do is populate a vector and an array. If I print the value of the variable I get the expected value. If I send it into a .txt I get the expected value. If I send just the value into Excel I get the expected value.
It all just breakes down when I send the value from the containers into Excel. Why can this be happening?
What seems likely to be the problem here is the way the Excel is interpreting the (formatted) numerical output from your c++ program. Even though the text may be correct (from the point of view of the cout function) it may not have the 'correct' decimal point character in it (i.e. a dot instead of a comma, or vice versa).
Solution: Make sure that Excel is set to use the same "locale" as the default c++ locale, or set the c++ locale to whatever Excel is using.
The MS-XLS file format is not a simple text file. You cannot simply put text into it and expect it to show up correctly. You would need more code and/or specialized libraries to interact with it.
See the suggestions here.
I am trying to pass a string from SV to C++ function but the value is not getting passed properly to C++ function
Code in SV side:
import "DPI" function string mainsha(string str);
class scoreboard ;
string text_i_cplus;
string text_o_cplus;
text_i_cplus="abc";
text_o_cplus=mainsha(text_i_cplus);
That's how I am sending value to C++ . At the C++ side I am taking value as:
extern "C" string mainsha(string input)
{
string output1 = sha256(input);
cout << "sha256('"<< input << "'):" << output1 << endl;
return output1;
}
I am getting correct output when I running the C++ prog alone. But at the console I am getting the following output:
sha256(''):e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
Can someone please suggest where I am going wrong or am I missing something?
The DPI in current SystemVerilog standard only supports the C language, not C++. In fact you should be using import "DPI-C". in C++, string is a class and C only has arrays of char
extern "C" const char * mainsha(const char * input)
{
string output1 = sha256(input); // not sure if you need a cast here
cout << "sha256('"<< input << "'):" << output1 << endl;
return output1.c_str();
}
If you are using Modelsim/Questa, there is a -dpiheader switch that you can use that automatically generates the DPI prototypes for you and you can include that file when you compile your C++ code. That way you will get a compiler error if there is a mismatch and not have to debug run-time behaviors.
I have a string that the user inputs, for example: 4-x^2.
createReadableFunction takes in that string and outputs a string that changes the first letter, in this case x, to (a+x*d)
functionString = createReadableFunction(functionString);
cout << "Function is: " << functionString << endl;
So now the functionString is 4-(a+x*d)^2
I want this string to define a float such as
_function = std::atof(functionString.c_str());
However, this function simply sets _function as 4 rather than 4-(a+x*d)^2
tldr; I want a float to be defined like _function = 4-(a+x*d)^2, but by converting a string to the float.
As already suggested, you actually want to evaluate mathematical expressions. This cannot be done out of the box in C++, but there are parsers/evaluators like this one, which can help you achieve what you want.
Give it a try and come back with a trial code, if something goes wrong.
i hope you can help me.
The thing is that i don't know how to change the value of a variable, for example, i have a
"char" variable and then i wanna change it to "int" or "float"
This is the code
#include<iostream>
#include<cstdlib>
using namespace std;
main()
{
{
cout<<" El valor de las calificaciones es A=10,B=9,C=8,D=7,E=6,F=5,G=4,H=3,I=2,J=1 " <<endl;}
char calificaciones[4];
int resultado,A=10,B=9,C=8,D=7,E=6,F=5,G=4,H=3,I=2,J=1, i, promedio;
for(i=1;i<4;i++)
{
cout<<"Ingrese calificacion con letra "<<i;
cin>>calificaciones[i];
}
promedio=(calificaciones[1]+calificaciones[2]+calificaciones[3])/3;
cout<<"El promedio de sus tres calificaciones es "<<promedio<<endl;
system("pause");
}
The program is supposed to ask for the user to enter three scores and the scores are shown in letters as you can see, A=10, B=9, etc, and once the user enters three letters the program is going to divide them into three, but since the variable "calificaciones" was a string first, how do i make the operation i want to do this, or whats the command that i could use for the program to understand that the user entered three letters and an operation will be made with them?
Hope you can help me and thanks.
If your original question is, how to change datatype, sorry that is not possible.
Although, what you are trying to achieve can be done by std::map
Create Map of your grades.
std::map<char,int> myGrades;
myGrades.insert ( std::pair<char,int>('A',10) );
myGrades.insert ( std::pair<char,int>('B',9) );
myGrades.insert ( std::pair<char,int>('C',8) );
myGrades.insert ( std::pair<char,int>('D',7) );
Read input: (this is same. only change is index starts from 0)
for(i=0;i<3;i++)
{
cout<<"Ingrese calificacion con letra "<<i;
cin>>calificaciones[i];
}
Get actual integers from map.
int total_grades = ( myGrades.find(calificaciones[0])->second +
myGrades.find(calificaciones[1])->second +
myGrades.find(calificaciones[2])->second);
promedio=total_grades /3.0; //<-- NOtice 3.0 to avoid int/int
It's impossible to change the datatype of a variable in strongly-typed languages like C++, Java, etc. You'll need to define a new variable with the desired type instead. Weakly-typed languages like Python and PHP are (generally) typeless and will let you mix and match datatypes however you like, but it's not possible in C++. You can technically use void pointers to point to objects of any type, but they don't let you change the type of existing variables. Here is more information on strong and weak typing.
If you're okay with creating a new variable, you can use conversion functions or manually convert between datatypes (if possible). For example, it's not possible to convert the string "Hello world" to an int, but you can change a string like "42" to an int. The cstdlib / stdlib.h header provides functions like atof() and atoi() which can do basic conversions (make sure you convert any C++ strings to character arrays using myString.c_str() before passing them). stringstream is also a very powerful tool which easily converts practically anything to a string, among other uses.
I'm not quite sure what you want to do, but you can use the ASCII values of the characters to convert them. For example, the letter A has the ASCII value of 65, B is 66, C is 67, and so on. Because characters are inherently stored as numbers, you can convert them without using special conversion functions. You can simply assign a char to an int:
char ch = 'A';
int x = ch; // this is an implicit conversion
cout << x << endl; // this prints '65'
The character is being cast to an integer. You can also explicitly convert it:
char ch = 'A';
cout << ch << endl; // this prints 'A'
cout << (int) ch << endl; // this prints '65' because of the explicit conversion
It also works the other way around:
int x = 65;
char ch = x;
cout << ch << endl; // this prints 'A'
I cannot get the atof() function to work. I only want the user to input values (in the form of decimal numbers) until they enter '|' and it to then break out the loop. I want the values to initially be read in as strings and then converted to doubles because I found in the past when I used this method of input, if you input the number '124' it breaks out of the loop because '124' is the code for the '|' char.
I looked around and found out about the atof() function which apparently converts strings to doubles, however when I try to convert I get the message
"no suitable conversion function from std::string to const char exists".
And I cannot seem to figure why this is.
void distance_vector(){
double total = 0.0;
double mean = 0.0;
string input = " ";
double conversion = 0.0;
vector <double> a;
while (cin >> input && input.compare("|") != 0 ){
conversion = atof(input);
a.push_back(conversion);
}
keep_window_open();
}
You need
atof(input.c_str());
That would be the "suitable conversion function" in question.
std::string::c_str Documentation:
const char* c_str() const;
Get C string equivalent
Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.
You can also use the strtod function to convert a string to a double:
std::string param; // gets a value from somewhere
double num = strtod(param.c_str(), NULL);
You can look up the documentation for strtod (e.g. man strtod if you're using Linux / Unix) to see more details about this function.