简体   繁体   中英

Change the value of a variable in C++?

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.

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM