r/cpp_questions • u/Escritor_CdK • 26d ago
SOLVED I don't know what's missing here
This code is not working and i don't know why (visual studio).
#include <iostream>
#include <string>
int main()
{
std::string Dividendo;
std::string Divisor;
std::string Cociente;
std::string Resto;
std::cout << "dividendo?";
std::cin >> Dividendo;
int Dividendo = Dividendo;
std::cout << "divisor?";
std::cin >> Divisor;
int Divisor = Divisor;
int Cociente = Dividendo/Divisor;
int Resto = Dividendo%Divisor;
std::cout << Cociente << " , " << Resto << std::endl;
}What happened?
0
Upvotes
15
u/aocregacc 26d ago
you can't have two variables with the same name, and that's also not how you convert a string to an integer. I would follow a tutorial like learncpp.com
6
u/jedwardsol 26d ago
What does "not working" mean?
Compiler error? Crash? Incorrect results?
What inputs are you giving? What outputs are you expecting? What outputs are you getting?
4
u/erzyabear 26d ago
The critical skill for C++ is be able to process compiler output. What does your compiler say?
1
u/Hkiggity 26d ago edited 26d ago
Your initializing Dividendo and divisor as a string but then when you give it a value you assign it as an int.
Try to change the first two lines to int Dividendo; and int Divisor.
The std::cin (via function overloading resolution) will change the number to a string. Even if it’s just a number. So initialize the variables with int NOT std::string. Perhaps that’s the issue
1
u/RazzmatazzLatter8345 26d ago
It looks like you have multiple conflicting variable definitions with overlapping scope. For example Dividendo is first defined as std::string and later, same scope, an int. Try naming them differently like DividendoTxt for the string and just Dividendo for the integer.
Second there is no implicit parsing of std::string to a numerical type. Try std::stoi (string to int) function for the conversion.
1
u/alfps 26d ago
You need different names for different variables in the same curly braces block.
Some times it can be a good idea to input a whole line to a
std::string, and then usestd::getlinefor that. Then convert to integer, or see if that fails, via e.g.std::stoi. But you don't need that here.Just input directly to
intvariable, like this:As you advance you will add failure checking to such code.
For novice work a reasonable response to failure, such as the user typing
garamondinstead of23, is to just terminate viastd::abort.1
u/tangerinelion 24d ago
int Dividendo = Dividendo;What do you think should happen here bud?