0

My C++ code compiles and runs, but no output is printed to the console. If I run this exact code on an online c++ compiler it works as expected. This problem occurred when I started adding everything related to arrays (the functions and so on) so the problem might be there as It was working fine before that. Also, I'm using GNU GCC Compiler for VSC and extra parameters when launching the exe. --rand to generate random numbers in the array. I've been trying to find the answer now for this for hours, and can't figure it out.

#include<iostream>
#include<iomanip>
#include<limits>
#include<stdio.h>
#include<stdlib.h>
#include <getopt.h>
#include<new>

int getNumber()
{
    int x;
    while(!(std::cin >> x)){
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Wrong Input. Try Again: ";
    }
    return x;
}

void outputArray(int rows,int columns,float array[]) 
{
    int i,j;
    for (i = 0; i < rows; i++){
        for (j = 0; j < columns; j++) 
        {
            std::cout << (i+1) <<" "<< array[i*columns+j] << std::endl;
        }
    }
}

float ArrayManual(int size,float array[])
{
    int i;
    std::cout << "Manual number input!" << std::endl;
    for (i = 0; i < size; i++){
                array[i] = getNumber();
            }
    return *array;

}

float ArrayRand(int size,float array[])
{
    int i;
    std::cout << "Parameter --rand found, numbers will be random!" << std::endl;
    float max = 50;
        for (i = 0; i < size; i++){
                array[i] = max * ((float)rand()/(float)RAND_MAX) * 2 - max;
            }
        return *array;
}

bool ProcessArgs(int argc, char** argv)
{
    const char* const short_opts = "r";
    const option long_opts[] = {
            {"rand", no_argument, nullptr, 'r'},
            {nullptr, no_argument, nullptr, 0}
    };
    while (true)
    {
        const auto opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);
        if (-1 == opt) {
            break;
        }
        switch (opt)
        {
        case 'r':
            return true;
            break;
        case '?': // Unrecognized option
            std::cout << "Unrecognised parameter!\n--rand/--r For random numbers.\n<none> For manual input.\n";
        break;
        default:
            return false;
            break;
        }
    }
}

int main(int argc, char **argv)
{
    std::cout << std::fixed;
    std::cout << std::setprecision(2);//set decimal point 2 0's
    bool x;
    int rows,columns;
    x=ProcessArgs(argc, argv);
    std::cout << "Enter Row count for array: ";
    rows=getNumber();
    std::cout << "Enter Column count for array: ";
    columns=getNumber();
    int size=rows*columns;//define array size
    //create new array
    float *array = new float[size];
    if(x==false) {
        *array=ArrayManual(size,array);
    } else {
        *array=ArrayRand(size,array);
    }
    outputArray(rows,columns,array);
    return 0;
}
  • I'll provide any extra information if needed because I'm not too well versed in this and don't know what to do. – PixelBreeze Oct 28 '18 at 16:17
  • Unrelated to your problem, but in your `ArrayRand` and `ArrayManual` functions, why do you return the first element of the arrays? And then assign that value to the first element of the very same array? It seems you could need [a good book or two to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), especially the chapters about arrays and pointers. – Some programmer dude Oct 28 '18 at 16:26
  • @Someprogrammerdude well my guess was that I would return the whole built array with values and assign it the the array in the main. Never really done c++ – PixelBreeze Oct 28 '18 at 16:32
  • Then the note about the books is doubly important. – Some programmer dude Oct 28 '18 at 16:33
  • 2
    Learning a programming language and guessing are nearly mutually exclusive. – user4581301 Oct 28 '18 at 16:37
  • @Someprogrammerdude well i took an example off of here, is it not the same? I create an empty array and then just pass back the whole array as a return. https://www.tutorialspoint.com/cplusplus/cpp_return_arrays_from_functions.htm – PixelBreeze Oct 28 '18 at 16:40
  • You aren't returning a pointer to the array (which is already available to the calling program!) You are returning the first element, a float. And your function declaration indicates it returns a float, not a pointer. These match or you would get a compile error. – doug Oct 28 '18 at 16:57
  • @doug i did look into this and did do some fix ups from looking in this https://stackoverflow.com/questions/12992925/c-correct-way-to-return-pointer-to-array-from-function But what is strange the code does compile and run on a online compiler, just not on my VS code. – PixelBreeze Oct 28 '18 at 17:40

0 Answers0