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");
}
Related
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 last year.
Improve this question
Im trying to create a a code that count numbers divisible by 9 by putting numbers into an array and count the numbers in it but it only prints 1 instead of the number of numbers divisible by 9 please help me i want to use array to count those numbers
#include <iostream>
using namespace std;
int main(){
int a,b,i;
int numbers[]={i};
cin >>a>>b;
for (i=a; i<=b; i++)
if (i%9==0){
cout << sizeof(numbers)/sizeof(numbers[0]);
}
}
Nowhere in your code are you adding numbers to the array. Anyhow, it is not possible to add elements to arrays, because they are of fixed size. Your array has a single element.
Moreover, int numbers[]={i}; is undefined, because i has not been initialized.
Further, it is not clear what is the purpose of sizeof(numbers)/sizeof(numbers[0]) in your code. sizeof(numbers) is the size of a single int because the array has a single element. sizeof(numbers[0]) is the size of a single int as well. Hence the result is 1 always. (Its a compile time constant btw.)
If you want to count how many numbers fullfil some condition you best use a counter and print its value after the loop:
#include <iostream>
int main(){
int a,b;
cin >> a >> b;
unsigned counter = 0;
for (int i=a; i<=b; i++) {
if (i%9==0){
++counter;
}
}
std::cout << counter;
}
i want to use array for my learning porpuses please help me
You chose the wrong example to train working with arrays, because as already mentioned, arrays have fixed size. It is an opportunity to learn about std::vector. You can add elements to a std::vector at runtime and query its size:
#include <iostream>
#include <vector>
int main(){
int a,b;
std::vector<int> by9divisables;
std::cin >> a >> b;
for (int i=a; i<=b; i++) {
if (i%9==0) {
by9divisables.push_back(i);
}
}
std::cout << by9divisables.size();
}
However, other than to see how std::vector is working, the vector has no place in this code. As you can see above, the result can be obtained without it.
This declaration
int numbers[]={i};
declares an array with only one element and initializes it with an indeterminate value stored in the variable i because the variable i was not initialized.
The body of this if statement
if (i%9==0){
cout << sizeof(numbers)/sizeof(numbers[0]);
}
does not make a sense because it always outputs the number of elements in the array numbers that has only one element. But according to the description of the assignment you have to place numbers divisible by 9 into the array.
As the user can enter arbitrary values for the variables a and b then it means that you need a variable length array. However variable length arrays is not a standard C++ feature. Instead you should use the standard container std::vector.
The program can look the following way
#include <iostream>
#include <vector>
#include <utility>
int main()
{
int a = 0, b = 0;
std::vector<int> numbers;
const int divisor = 9;
std::cout << "Enter two integer numbers: ";
std::cin >> a >> b;
if ( b < a ) std::swap( a, b );
for ( int i = a; not ( b < i ); ++i )
{
if ( i % divisor == 0 ) numbers.push_back( i );
}
std::cout << "There are " << numbers.size()
<< " numbers divisible by " << divisor
<< " in the range [" << a << ", " << b << "]\n";
if ( numbers.size() != 0 )
{
std::cout << "They are ";
for ( const auto &n : numbers )
{
std::cout << n << ' ';
}
std::cout << '\n';
}
}
Your code is printing one's for each element found because your array only has one element in it!
Print i instead like so:
#include <iostream>
using namespace std;
int main(){
int a,b,i;
//int numbers[]={i};
cin >>a>>b;
for (i=a; i<=b; i++)
{
if (i%9==0)
{
cout << "i: " << i << endl;
}
}
}
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.
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
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;
}
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 8 years ago.
Improve this question
I'm not sure if I have the pow function in the right place, but I can't figure out how to set months to -months powered.
Here is the original formula i am suppose to base this off of:
principal*(rate/12)/(1 - (1 + rate/12)^-months)
I am getting the error message:
error C2064: term does not evaluate to a function taking 1 arguments
Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int y = -1;
double months;
double principle = 1000;
double rate = 7.20;
double monthly_pay;
cout << "Please enter the number of months you have to pay back the loan:";
cin >> months;
monthly_pay = principle*(rate/12)/((1-pow((1+rate/12), -months)));
cout << monthly_pay << endl;
return 0;
}
double pow (double base, double exponent);
//Returns the value of the first argument raised to the power of the second argument.
so change
monthly_pay = principle*(rate/12)/(1-(1+rate/12)pow(months, y);
to
monthly_pay = principle*(rate/12)/((1-pow((1+rate/12), -months));
I broke your algorithm into smaller functions (I hope you don't mind). I came up with this code that compiled just fine for me. I believe you have the concept understood, you just forgot to multiply the pow(a,b) function using the asterisk.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int y = -1;
double months;
double principle = 1000;
double rate = 7.20;
double monthly_pay;
double a, b;
cout << "Please enter the number of months you have to pay back the loan:";
cin >> months;
a = rate / 12;
b = pow(months, y);
monthly_pay = (principle * a) / (1 - (1 + a) * b);
cout << monthly_pay << endl;
return 0;
}
I hope this helps!