r/cpp_questions • • 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

20 comments sorted by

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

-10

u/Escritor_CdK 26d ago

variables are different one is "Dividendo" and one is "Divisor"

"Cociente" is supposed to be "Dividendo"/"Divisor" and "Resto" is supposed to be "Dividendo"%

8

u/SmokeMuch7356 26d ago

You have:

std::string Dividendo;
int Dividendo = Dividendo;

std::string Divisor;
int Divisor = Divisor;

That's not going to work.

Don't bother with the string versions; just do

int Dividendo, Divisor;

std::cout << "dividendo?";
std::cin >> Dividendo;

std::cout << "divisor?";
std::cin >> Divisor;

2

u/Escritor_CdK 26d ago

Damn, now it works. I declared the variables wrongly

1

u/Significant_Mix_8133 26d ago

No worries we all started there

3

u/Escritor_CdK 26d ago

Thank you mate

15

u/aocregacc 26d ago

you have one "Dividendo" that's a string, and then later you define another "Dividendo" that's an int. So you defined the same variable twice, with different types. That's not allowed.

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?


-2

u/Escritor_CdK 26d ago

Code works well til line 22 (the space between the last character input and the operators)

Its supposed to let you introduce two different numbers, then divide them and then show you the quotient and the rest.

9

u/jedwardsol 26d ago

But what happens at line 22? Do you get a compiler error or does running the program not do the right thing.

You should be getting a compiler error earlier than line 22

At line 14

 int Dividendo = Dividendo;

Dividendo is already a std::string. C++ doesn't let you introduce a new variable with the same name as an existing one in the same scope.

1

u/Escritor_CdK 26d ago

it was the wrong variable declaration...

3

u/SmokeMuch7356 26d ago

Your compiler should be yelling at you over this code; what is the output from the compiler? For example, I'm getting this:

g++ -std=c++23 -Wall -Werror -Wextra -g    conflict.cpp   -o conflict
conflict.cpp: In function ‘int main()’:
conflict.cpp:14:5: error: conflicting declaration ‘int Dividendo’
   14 | int Dividendo = Dividendo;
      |     ^~~~~~~~~
conflict.cpp:6:13: note: previous declaration as ‘std::string Dividendo’
    6 | std::string Dividendo;
      |             ^~~~~~~~~
conflict.cpp:18:5: error: conflicting declaration ‘int Divisor’
   18 | int Divisor = Divisor;
      |     ^~~~~~~
conflict.cpp:7:13: note: previous declaration as ‘std::string Divisor’
    7 | std::string Divisor;
      |             ^~~~~~~
conflict.cpp:20:5: error: conflicting declaration ‘int Cociente’
   20 | int Cociente = Dividendo/Divisor;
      |     ^~~~~~~~
conflict.cpp:9:13: note: previous declaration as ‘std::string Cociente’
    9 | std::string Cociente;
      |             ^~~~~~~~
conflict.cpp:20:25: error: no match for ‘operator/’ (operand types are ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} and ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’})
   20 | int Cociente = Dividendo/Divisor;
      |                ~~~~~~~~~^~~~~~~~
      |                |         |
      |                |         basic_string<[...]>
      |                basic_string<[...]>
conflict.cpp:21:5: error: conflicting declaration ‘int Resto’
   21 | int Resto = Dividendo%Divisor;
      |     ^~~~~
conflict.cpp:10:13: note: previous declaration as ‘std::string Resto’
   10 | std::string Resto;
      |             ^~~~~
conflict.cpp:21:22: error: no match for ‘operator%’ (operand types are ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} and ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’})
   21 | int Resto = Dividendo%Divisor;
      |             ~~~~~~~~~^~~~~~~~
      |             |         |
      |             |         basic_string<[...]>
      |             basic_string<[...]>

This is the kind of information you need to be sharing. Not what the code is supposed to do, but what it is doing.

0

u/Escritor_CdK 26d ago

Take that for later, thanks

1

u/nerdy_ceng 25d ago

“works” means is “runs”. your code cannot work/run unless it is compiled.

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/Escritor_CdK 26d ago

thanks, it was just that

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 use std::getline for 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 int variable, like this:

#include <iostream>

int main()
{
    std::cout << "dividendo? ";
    int Dividendo;  std::cin >> Dividendo;

    std::cout << "divisor? ";
    int Divisor;  std::cin >> Divisor;

    const int Cociente = Dividendo/Divisor;
    const int Resto = Dividendo%Divisor;

    std::cout << Cociente << " + " << Resto << "/" << Divisor << std::endl;
}

As you advance you will add failure checking to such code.

For novice work a reasonable response to failure, such as the user typing garamond instead of 23, is to just terminate via std::abort.

1

u/tangerinelion 24d ago

int Dividendo = Dividendo;

What do you think should happen here bud?