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;
}