How to find a remainder [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I went about this trying by using the modulo (%) operator. But each time I see a message that my application has stopped working. Here's my code:
using namespace std;
int main ()
{
int A;
int B;
int C;
C = A % B;
cout << "What is A";
cin >> A;
cout << "What is B";
cin >> B;
cout << A % B;
return 0;
}

int A;
int B;
int C;
C=A%B;
So, you calculate C based on values you did not even set yet,A and B. They can be anything, and hence, what they actually are is undefined, and so is what happens when you calculate A%B. Probably B happens to be 0, and that yields an arithmetic error in your CPU.

It is undefined behaviour to read a variable that has not been initialized, such as your A, B and C.

Your variable B isn't initialized with a value, but the compiler seems to be so kind to set it to 0, so A%B does (internally) a division by zero which isn't a valid math operation, so a critical error occurs.

Welcome to C++! :)
Make use of C since you are computing it:
cout << C << endl; //outputs the computation of A % B
In conclusion, here is an edited version of you snippet.
#include <iostream> //used for cout and cin
using namespace std;
int main ()
{
int A; //A,B,C are initialized with no values
int B;
int C;
cout << "What is A";
cin >> A; //A is given a value
cout << "What is B";
cin >> B; //B is given a value
C = A % B; //previous place in the code is computing empty values. but placing this line of code AFTER values have been set, allows the program to compute.
cout << C;
return 0;
}

Related

how to fix return in recursive function? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I made a recursive function that calculates factorial.
When the values ​​of a and b == int everything is OK. BUT when I changed the value of b to long double, everything broke.
Where did I make a mistake?
gives -nan (ind) instead of result
error in return?
#include <iostream>
using namespace std;
long double factorial(int, long double);
int main()
{
int a;
cout << "enter number: " << endl;
cin >> a;
long double b = 1.0;
cout << "\n factorial " << a << " = " << factorial(a, b) << endl;
return 0;
}
long double factorial(int a, long double b)
{
if (a && (a - 1))
{
b = b * a * (a - 1);
cout << "a:" << a << " b: " << b << endl;
a = a - 2;
factorial(a, b);
}
else if ((a - 1) == 0) { cout << "b final return = " << b; return b; }
}
console output
Consider these:
A function whose return type isn't void must always return a value. Your function has no return statement in the if branch, nor outside the if-statement. As a consequence, the behaviour of the program is undefined if the branch is ever entered. To fix this, you must add a return statement.
Considering the function has no side-effects, calling it is entirely pointless unless you use the returned value. Your intention may be to return factorial(a, b);, which makes the call useful, and fixes the lack of return statement mentioned previously.
Also, the function lacks another return statement if neither the first if branch, nor the else-if branch is entered.
(a - 1) == 0
That's one unnecessarily complicated way of expressing a == 1.
factorial(a, b);
should be
return factorial(a, b);
Your version calls the factorial recursively but does nothing with the result. You need to return the result of the recursive call from the current call.

How to convert data[i].int into a vairable [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
So I'm kind of new to c++ and I'm currently working with strings. and I want to input some amount and compare them to each other, but since i have them in data type in arrays it wont let me do the substrcution and I don't understand why
for (int i=0;i<N;i++)
{
cout << "Name"<< endl;
cin >> data[i].name;
cin >> data[i].all;
cin >> data[i].con;
}
exceed = data[i].con-data[i].all;
while (exceed > maxvalue){
maxindex = -1;
maxvalue = exceed;
if (maxvalue > 0){
cout << data[i].name;
}
Without knowing what type or struct or class you're using for your data member, or what error you're encountering it's hard to tell you what exactly is going on. You are also referencing i outside of your for loop so that may be your issue.
I've recreated a short program that seems to be doing what you're going for with a simple struct. Because the struct defines con and all as int types, they are converted on input, and i is no longer referenced outside of the for loop.
#include <iostream>
#include <string>
struct dataType {
std::string name;
int all;
int con;
};
int main() {
int N = 2;
int maxValue = 3;
dataType data[N];
for (int i = 0; i < N; ++i) {
std::cout << "Name" << std::endl;
std::cin >> data[i].name;
std::cin >> data[i].all;
std::cin >> data[i].con;
int exceed = data[i].con - data[i].all;
if (exceed > maxValue) {
std::cout << data[i].name << std::endl;
}
}
}
If you are using a struct or something where con and all are strings, there is a method in std::string stoi that can convert string types to int. Below is a short example.
int x;
std::string test = "4";
x = std::stoi(test);
std::cout << x << std::endl;
Note that an invalid argument in stoi throws an exception, but as a beginner you probably haven't learned about exception handling yet (but you should once you get the hang of things).
Hope that helps, cheers.

Bracket (Expected a declaration) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am new to the community but I need help concerning on brackets expecting a declaration and a user defined function not being found
//Problem 1.1
#include <iostream>
#include <cmath>
using namespace std;
int intPow(int base, int exponent); // this one has a green line
int main() {
int Base, Expo,final;
cout << "Enter Base value:";
cin >> Base;
cout << "Enter Exponent Value";
cin >> Expo;
final = intPow(Base, Expo);
cout << "Base Exponent of given value:" << intPow;
system("pause");
}
int intPow(int base, int exponent);//and this got a greenline to, telling me that It is not found
{ //and this one got a redline expecting me to put a declaration
for (int a = 0; a <= Expo; a++)
return intPow;
}
I am coding this on Visual Studio 2017 C++
Thank you for the help
This is not the answer to the original question, just a correction of the function implementation:
It does not check for arithmetic overflow though. So, intPow(10, 100) will fail. Also a negative exponent will fail (returning 1 for any negative value).
int intPow(int base, int exponent)
{
int result = 1;
for (int a = 0; a < exponent; a++) // loop 'exponent' times
result *= base;
return result;
}
The original implementation has some problems:
The Expo variable is defined within the main function
and therefore not visible within this function
The for-loop runs once too often
Within the for-loop, the function returns calling the function itself once again (without parameters though)
First of all note that you are doing:
cout << "Base Exponent of given value:" << intPow;
instead of
cout << "Base Exponent of given value:" << final;
Now the problem you are describing is that when implementing the function it is expecting to find a block of code delimited by brackets right after the definition int intPow(int base, int exponent). Instead you are putting a semicolonon:
Just do :
int intPow(int base, int exponent)//and this got a greenline to, telling me that It is not found
{ //and this one got a redline expecting me to put a declaration
for (int a = 0; a <= Expo; a++)
return intPow;
}
ok, I finally sort and used the functions and avoided some error thank you to #RobertKock and #FrancescoBoi.
The task given to me was to insert a base number and its exponents after that I should show the quantity of the exponents that was in the base kind of like this. 4,3 (4^3) = 4*4*4.
I almost had the code correctly, the only problem is the character "*" is following along the for loop.
#include <iostream>
#include <cmath>
using namespace std;
int intPow(int digits, int exponent)
{
int result = 1;
for (int a = 0; a < exponent; a++)
cout << digits<<"*";
result = digits;
return result;
}
int main()
{
int Base, Expo,final;
cout << "Enter Base value:";
cin >> Base;
cout << "Enter Exponent Value:";
cin >> Expo;
final = intPow(Base, Expo);
system("pause");
}

I keep getting the same answer, no matter what I put in [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
We're doing an assignment in my computer science class that requires us to find the future value of an investment after "n" number of years that the user inputs. It's written in C++. This is the code as I have it now:
#include <iostream>
using namespace std;
int main() {
int P=1000;
int i=0.0275;
int FV;
int n;
cout << "enter number of years:"<< endl;
cin >> n;
cout << "the future value is:"<< endl;
FV = P*(1+(i*n));
cout << FV << endl;
return 0;
}
I keep ending up with an answer of 1000 no matter what "n" I input. Can someone tell me what's wrong with the code?
#include <iostream>
using namespace std;
int main() {
int P=1000;
float i=0.0275; //float instead of int
float FV; //FV should also be float as it will be storing decimal values
int n;
cout << "enter number of years:"<< endl;
cin >> n;
cout << "the future value is:"<< endl;
FV = P*(1+(i*n));
cout << FV << endl;
return 0;
}
the mistake you have done is the type you assigned to your variables! as int only handles integer values i becomes 0 and your result becomes 1000! use float instead of int for numbers with decimal points!
Datatype of i is int as a result of which your floating point value of i will be rounded of to 0 and you will end up getting the same output doesn't matter what your n value is. Change the datatype of your i and FV varaiable from int to float then your output changes based on what n value you key in

C++ for loops "Statement has no effect" warning? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
so I just started learning programming with C++ and I'm currently messing with basic console programs. I wanted to make a little spam program. here's the code :
#include <iostream>
#include <string>
using namespace std;
string a;
int b;
void repetition(){
cout << "Please enter the number of time you want the text to be spammed" << endl;
cin >> b;
}
void text(){
cout << "Please enter the text you want to spam." << endl;
cin >> a;
for(;b == 0;){
cout << a << endl;
b - 1;
}
}
int main()
{
cout << "Welcome to your auto-spammer!!" << endl;
repetition();
text();
return 0;
}
I'm getting a warning saying "statement has no effect" for my for statement at line 20. I wanted to know why and how I could fix this. Thank you.
The for loop executes while the second statement is true. So unless you enter 0, it will never execute.
The warning is for b - 1; . This reads the value of b, subtracts 1, and does nothing with the result. You probably meant b = b - 1; (which can also be written as b -= 1;, or --b;).
I'm guessing this is line 20:
b - 1;
That line by itself does nothing. The result of b-1 is never assigned to anything.
Try --b, which will decrement b by 1 and re-assign the result of that operation to b.
In text(), b-1 indeed does nothing, you probably meant --b. The first returns an rvalue which is then discarded, while the second decrements b by one and results in b (though you should look up the difference between --b and b-- to understand how that statement actually works). That said, the more colliquial way to do it is like this:
for(; b > 0; --b) //Also keep in mind that the second section of a for statement
//is the continue condition, not exit
cout << a << endl;
You want to do print the text N number of times, so the proper loop to use is:
for (int i=0; i < b; i++)
cout<<a<<endl;
Modifying b is generally not a good idea, you might need the value the user entered later on.