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.
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 2 years ago.
Improve this question
#include <iostream>
int main() {
int manic;
std::cout << "Enter Size of Array" << "/n";
std::cin >> size; // << "/n";
size = manic;
static const int arr[size];
for(int i = 0; i < size; i++) {
std::cout << "Enter" << i << "Element" << "/n";
std::cin >> (arr[i]); // Error Is Shown in this Line
}
bool r = is_even(arr, size);
std::cout << r;
return 0;
}
My first post here .I typed this code in Visual Studio 2019.The Microsfot Documents do not help.
The compiler gives you the hint that you may uninentionally override a value
std::cin >> size;// << "/n";
size = manic;
You let the user input a value and then override it with another, by the way uninitialized, value.
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");
}
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 have to make a program in C++ that will read numbers and then arrange them in ascending order. The numbers can be infinite, so the program should read numbers until any particular value is entered to terminate the reading process. I have written below code but is not working and showing undesired output. I will be so thankful if someone will help me.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *p,*q,i=1,j,k,temp;
p=(int*)malloc(sizeof(int));
cin>>*p;
while((*p)!=-1) //read until -1 is entered
{
i++;
p=(int*)realloc(p,sizeof(int)*i);
q=p;
p=p+(i-1); //going to next address to read
cin>>*p;
}
p=q;
for(j=1;j<i;++j)
{
for(k=0;k<i-j-1;++k)
{
if((*(p+k))>(*(p+k+1)))
{
temp=*(p+k);
*(p+k)=*(p+k+1);
*(p+k+1)=temp;
}
}
}
cout<<"\n";
for(j=0;j<i-1;++j)
{
cout<<*(p+j)<<" ";
}
}
Expanding on my comment, here is what an actual C++ solution might look like:
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> numbers;
int number = -1;
std::cin >> number;
while (number != -1)
{
numbers.push_back(number);
number = -1;
std::cin >> number;
}
std::sort(numbers.begin(), numbers.end());
for (int x : numbers)
{
std::cout << x << ' ';
}
std::cout << '\n';
}
After
p=p+(i-1);
p is no longer a pointer that is valid to realloc.
Replace
p=p+(i-1);
cin>>*p;
with
cin >> p[i-1];
and get rid of q.
(You can use cin >> *(p + i - 1); if you insist on obfuscation.)
Your sorting routine also becomes much more readable if you replace the pointer arithmetic with indexing.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to find greatest number entered, and then display it to the user.
So once the user enters several numbers, program calls function that will find it and then return it. Unfortunately every time I run it, the number resets to zero even after the function has worked successfully. What do I do wrong?
Visual Studio mentions this: Run-Time Check Failure #3 - The variable 'gromax' is being used without being initialized.
int largestGroup(int groupsize[], int theValue)
{
int gromax=groupsize[0];
for (int i=1;i<9;i++){
if(groupsize[i] > gromax){
gromax=groupsize[i];
}
}
return gromax;
}
int main()
{
int groupsize[10]={0,0,0,0,0,0,0,0,0,0};
int gromax = groupsize[0];
char newentry='n';
do{
cin >> groupsize[i];
cout << string(60, '\n');
cout << "Would you like to enter another questionare? Enter either 'y' or 'n': " << endl;
cin >> newentry;
cin.ignore();
}while((newentry =='y') || (newentry=='Y'));
largestGroup(groupsize, i);
cout << "Number of customers in largest group today was " << gromax << endl;
Insert in the very beginning of main
int i = 0;
and inside the loop increase it as for example
cin >> groupsize[i++];
Change this
largestGroup(groupsize, i);
statement to
int gromax = largestGroup(groupsize, i);
And remove statement
int gromax = groupsize[0];
Also you shall check in the loop that you are not trying to acces a memory beyond the array.
And function largestGroup is wrong.
int largestGroup(int groupsize[], int theValue)
{
int gromax=groupsize[0];
This declares a function that - despite appearances - actually takes an array and an integer (inherited from C, arrays can decay to pointers as necessary, and it's deemed necessary when someone tries to pass them to functions).
Then, it declares a private local variable, gromax. This value, think of it as largestGroup::gromax, exists only for the life time of each individual call to the function. The last line of the function is then paramount.
return gromax;
}
This pushes the "gromax" into whatever CPU register/location return values are stored. It will live there until something else uses the value.
The language does not automatically transfer the value anywhere, even if your calling function has a variable of the same name.
So, the bug in your code is this:
largestGroup(groupsize, i);
You call the function, and you never capture the return value.
gromax = largestGroup(groupsize, i);
would capture the value.
Be aware that arrays are passed by address/pointer (live demo: http://ideone.com/DTkbFn)
#include <iostream>
void f(int groups[5], int x)
{
x = 3;
groups[x] = 999;
}
int main()
{
int groups[5] = { 0, 1, 2, 3, 4 };
int x = 10;
std::cout << "before: x = " << x << ", groups[3] = " << groups[x] << '\n';
f(groups, x);
std::cout << "after: x = " << x << ", groups[3] = " << groups[x] << '\n';
return 0;
}
When you pass an array, you are actually passing by pointer, so normal "pass by value" behavior does not apply.
int largestGroup(int groupsize[], int thevalue)
is actually equivalent to
int largestGroup(int* groupsize, int theValue)
It is advisable to use the second syntax to avoid falling into the trap of thinking you can modify groupsize with no impact on the caller.
You didn't use the return value. Try:
gromax = largestGroup(groupsize, 1);