-1

I have seen another post with nearly the same title but it didn't help at all since they had a mistake in there code. I just want to get as many values as the client puts in and add them but it only takes the first input

#include <iostream>
#include <string>
using namespace std;

int issa_def() {
    int issa[] = {};
    int half_ans_issa[] = {};
    int i  = 0;
    int cn = 0;
    while(true) {
        cout << "enter prices of items ordered by issa: ";
        cin >> cn;
        if(cn == 111) {
            break;
        }
        else {
        issa[i] = cn;
        }
        i++;
        cin.clear();
    }
    int a = 0;
    for(int i = 0;i <= sizeof(issa);i+=2) {
        int ans = issa[i] + issa[i+1];
        half_ans_issa[a] = ans;
        a++;
        cout << issa[i] << "\n" << issa[i+1] << "\n";
    }
    a = 0;
    cout << half_ans_issa[a];
}
int main() {
    issa_def();

}

here is the output after printing the values of the first and second input i put in and the sum

C:\Users\akram\Documents\code>a.exe
enter prices of items ordered by issa: 5
enter prices of items ordered by issa: 5
enter prices of items ordered by issa: 111
first input:5
second input:0
output:5

note: 111 is the exit code

i turned on compiler warnings as stated by user –πάντα ῥεῖ and i got the error

dahra.cpp:23:18: warning: comparison between signed and unsigned integer express
ions [-Wsign-compare]
  for(int i = 0;i <= sizeof(issa);i+=2)
  • Your code has undefined behavior. Where's your `return` statement in that function? Switch all compiler warnings on before askineg here next time please. – πάντα ῥεῖ Aug 08 '21 at 05:41
  • may i ask how would the compiler warnings would help me in this situation – Tsutsui Tsutsun Aug 08 '21 at 05:43
  • You'll get a warning about that missing `return` statement. Put one there. – πάντα ῥεῖ Aug 08 '21 at 05:44
  • 1
    Next problem are zero sized arrays: `int half_ans_issa[] = {};` more UB all the way. I'D recommend you' start with a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – πάντα ῥεῖ Aug 08 '21 at 05:52
  • Use a `std::vector` when you need a dynamic array whose size is not known at compile-time and will grow at runtime. – Remy Lebeau Aug 08 '21 at 05:53
  • i do give it a value later on but i just wrote it this way as the size is user defined i cannot state the size beforehand – Tsutsui Tsutsun Aug 08 '21 at 05:53
  • ill try the std::vector and will reply with an answer – Tsutsui Tsutsun Aug 08 '21 at 05:54
  • @TsutsuiTsutsun you can't use arrays like that. They need to have a fixed size at compile-time. Use `std::vector` instead. – Remy Lebeau Aug 08 '21 at 05:54
  • i got it the nature of arrays do not allow this and vectors are better at this so i will learn vectors and change my code thank you – Tsutsui Tsutsun Aug 08 '21 at 05:57
  • What is the size of your arrays? You initialize them with {} so they are just an int* without any memory allocation. As soon as you insert values to issa[i] you will overflow. For a similar reason you cannot iterate from 0 to sizeof(issa) (including the latter). First, sizeof gives you the size in bytes, not the number of array elements. Second, should sizeof works as you expect, you iterate through (n+1) elements, not (n). Finally, if the array has an odd number of elements, you access memory outside the array (issa[i+1]). You should use std::vector to get an arbitrary number of inputs. – xuman Aug 08 '21 at 06:02
  • @xuman can you post your comment as an answer so that i can flag it as the answer thank you all for your help it really helped me – Tsutsui Tsutsun Aug 08 '21 at 06:08

3 Answers3

1

What is the size of your arrays? You initialize them with {} so they are just an int* without any memory allocation. As soon as you insert values to issa[i] you will overflow.

For a similar reason you cannot iterate from 0 to sizeof(issa) (including the latter). First, sizeof gives you the size in bytes, not the number of array elements. Second, should sizeof works as you expect, you iterate through (n+1) elements, not (n). Finally, if the array has an odd number of elements, you access memory outside the array (issa[i+1]). You should use std::vector to get an arbitrary number of inputs.

Finally, you should have a return of your issa_def function.

Wrapping up, when you insert elements in your array you should use

issa.push_back(cn)

And, when iterating through issa, you should consider even/odd arrays, as well as the appropriate size of the array:

for(int i = 0; i < issa.size() - 1; i += 2) {
  ...
}
xuman
  • 133
  • 1
  • 3
  • 11
0

You should flush cin buffer with cin.clear(); cin.ignore();

cin.clear() clears the internal state of stream, this way you avoid errors. Then cin.ignore() ignores everything until the next line (that's what you want)

pleshw
  • 31
  • 1
  • 8
0

First of all, you have to define array issa with a constant size. Here you have defined an empty array. So you cannot access elements with issa[i] and assign a new value. For this purpose, you can use a vector array.

vector<int> issa;
int temp;
cin>>temp;
issa.push_back(temp);

2nd You have to write a return statement.

Warning: comparison between signed and unsigned

Here, the problem with your code is you are comparing a signed integer with an unsigned integer. sizeof(issa) returns an unsigned integer.

you can fix this by declaring a signed integer like

int n = sizeof(issa);
for(int i = 0;i <n ;i+=2) {

pawan
  • 113
  • 7