system("pause") not working, can anyone see what I'm missing? - c++

My program has errors and wont let me compile. The only errors start at system("pause) though but I dont see why I'm having errors because I did it the same as I always do my programs. Can anyone see what might be the issue? heres the code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
//Declarations
int SIZE = 10;
int NUMBERS[10];
int i;
int j;
int temp;
for (int i = 0; i < SIZE; i++)
{
cout << "Please enter a number: " << endl;
cin >> NUMBERS[i];
}
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (NUMBERS[j] > NUMBERS[j+1])
{
temp = NUMBERS[j];
NUMBERS[j] = NUMBERS[j+1];
NUMBERS[j+1] = temp;
}
}
}
cout << "Sorted List" << endl;
cout << "===========" << endl;
for (int i = 0; i < SIZE; i++)
cout << "Number " << i + 1 << ": " << NUMBERS[i] << endl;
}
system("pause");
return 0;
}

for (int i = 0; i < SIZE; i++) {
// ^^^ add this missing bracket
cout << "Number " << i + 1 << ": " << NUMBERS[i] << endl;
}
^^^ // closing bracket, but has no opening match
Of course you can skip braces in this case ( as there is only one single line in for body) so this is also solution:
for (int i = 0; i < SIZE; i++)
cout << "Number " << i + 1 << ": " << NUMBERS[i] << endl;

Related

Implementation of dynamic arrays

In my assignment, I am to find a sum grid using dynamic arrays. Here is my code without the use of dynamic arrays (I used a 2d array). I made the size dependent on user input. Can someone help me understand and implement dynamic arrays in this code? If you need, I can post all of my code to help you understand what I am trying to do.
void sumGrid(int array[], int& Size)
{
cout << "Sum Grid" << endl;
int a2d[Size][Size];
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
a2d[i][j] = array[i] + array[j];
}
}
cout << " " << setw(7);
for (int j = 0; j < Size; j++)
{
cout << setw(7) << array[j] << ' ';
}
cout << endl;
for ( int k = 0; k < Size; k++)
{
cout << array[k] << ' ';
for (int l = 0; l < Size; l++)
{
cout << setw(7) << a2d[k][l] << ' ';
}
cout << endl;
}
cout << endl;
}
The output of my code looks like:
Sum Grid
1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
The statement int a2d[Size][Size]; is defining a Variable-Length Array, since Size is not a compile-time constant. VLAs are not part of standard C++, so they should be avoided. The correct and standard way to allocate a dynamic array is to use the new[] operator (see 1, 2), eg:
void sumGrid(int array[], int& Size)
{
cout << "Sum Grid" << endl;
int **a2d = new int*[Size];
for (int i = 0; i < Size; i++)
{
a2d[i] = new int[Size];
for (int j = 0; j < Size; j++)
{
a2d[i][j] = array[i] + array[j];
}
}
cout << " " << setw(7);
for (int j = 0; j < Size; j++)
{
cout << setw(7) << array[j] << ' ';
}
cout << endl;
for ( int k = 0; k < Size; k++)
{
cout << array[k] << ' ';
for (int l = 0; l < Size; l++)
{
cout << setw(7) << a2d[k][l] << ' ';
}
cout << endl;
}
cout << endl;
for (int i = 0; i < Size; i++)
{
delete[] a2d[i];
}
delete[] a2d;
}
Alternatively, using a 1d array that mimics a 2d array, so that the entire array is allocated sequentially in memory and not scattered around memory:
void sumGrid(int array[], int& Size)
{
cout << "Sum Grid" << endl;
int *a2d = new int[Size*Size];
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
a2d[(i*Size)+j] = array[i] + array[j];
}
}
cout << " " << setw(7);
for (int j = 0; j < Size; j++)
{
cout << setw(7) << array[j] << ' ';
}
cout << endl;
for ( int k = 0; k < Size; k++)
{
cout << array[k] << ' ';
for (int l = 0; l < Size; l++)
{
cout << setw(7) << a2d[(k*Size)+l] << ' ';
}
cout << endl;
}
cout << endl;
delete[] a2d;
}
That being said, you should avoid using new[]/delete[] directly, as it is error-prone, and particularly risks leaking memory if an error occurs. Use the standard std::vector container instead, let it handle the memory management for you, eg:
Using a 2d sparse array:
#include <vector>
void sumGrid(int array[], int& Size)
{
cout << "Sum Grid" << endl;
std::vector<std::vector<int>> a2d(Size);
for (int i = 0; i < Size; i++)
{
a2d[i].resize(Size);
for (int j = 0; j < Size; j++)
{
a2d[i][j] = array[i] + array[j];
}
}
cout << " " << setw(7);
for (int j = 0; j < Size; j++)
{
cout << setw(7) << array[j] << ' ';
}
cout << endl;
for ( int k = 0; k < Size; k++)
{
cout << array[k] << ' ';
for (int l = 0; l < Size; l++)
{
cout << setw(7) << a2d[k][l] << ' ';
}
cout << endl;
}
cout << endl;
}
Using a 1d sequential array:
#include <vector>
void sumGrid(int array[], int& Size)
{
cout << "Sum Grid" << endl;
std::vector<int> a2d(Size * Size);
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
a2d[(i*Size)+j] = array[i] + array[j];
}
}
cout << " " << setw(7);
for (int j = 0; j < Size; j++)
{
cout << setw(7) << array[j] << ' ';
}
cout << endl;
for ( int k = 0; k < Size; k++)
{
cout << array[k] << ' ';
for (int l = 0; l < Size; l++)
{
cout << setw(7) << a2d[(k*Size)+l] << ' ';
}
cout << endl;
}
cout << endl;
}

Limiting the inputs and again asking the user for the right range

I'm stuck on this program: I have to limit the input but the program just totally ignore the if statement if i enter a value greater or equal to 100 it should
display an error but continue to ask user for input and run
Here is my code:
#include<iostream>
using namespace std;
int main()
{
int a[4][4], big1, n, m, i, j, loc1, loc2;
cout << "Enter no of rows and columns:";
cin >> m >> n;
cout << "Enter the array:\n";
if (n > 100 || m>100 )
{
cout << "Error! number should in range of (1 to 99)." << endl;
cout << "Enter the number again: ";
cin >> m >> n;
}
else
for (i = 0; i < m; i++)
{
for (j = 0; j < n; ++j)
{
cin >> a[i][j];
}
}
cout << endl << "Entered Matrix: " << endl;
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
{
cout << " " << a[i][j];
if (j == n - 1)
cout << endl << endl;
}
big1 = a[0][0];
loc1 = 0;
loc2 = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j<n; ++j)
{
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
if (a[i][j]>big1)
{
big1 = a[i][j];
loc1 = i;
loc2 = j;
}
}
}
cout << "\nLargest number:" << big1 << endl;
cout << "The position that had the largest number is in " << " Row " << loc1 << " " << "Column " << loc2 << endl;
system("pause");
return 0;
}
I've added a while to repeat the check until the number falls below 100. Please note the line "int a[4][4],.." will overrun if you exceed 4 rows * 4 columns. I suggest moving its initialization to a lower position when rows and columns are known.
Try this :
#include<iostream>
using namespace std;
int main()
{
int big1, n, m, i, j, loc1, loc2;
cout << "Enter no of rows and columns:";
cin >> m >> n;
cout << "Enter the array:\n";
while (n > 100 || m>100 )
{
cout << "Error! number should in range of (1 to 99)." << endl;
cout << "Enter the number again: ";
cin >> m >> n;
}
int a[m][n];
//else redundant
for (i = 0; i < m; i++)
{
for (j = 0; j < n; ++j)
{
cin >> a[i][j];
}
}
cout << endl << "Entered Matrix: " << endl;
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
{
cout << " " << a[i][j];
if (j == n - 1)
cout << endl << endl;
}
big1 = a[0][0];
loc1 = 0;
loc2 = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j<n; ++j)
{
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
if (a[i][j]>big1)
{
big1 = a[i][j];
loc1 = i;
loc2 = j;
}
}
}
cout << "\nLargest number:" << big1 << endl;
cout << "The position that had the largest number is in " << " Row " << loc1 << " " << "Column " << loc2 << endl;
system("pause");
return 0;
}

How to read and write a 3D array from text file using c++?

I'm currently making a program for school which involves a booking system for seats, and I'm trying to implement a system where seats are saved after every run of the program, and as soon as the program opens again, the current seats taken or available are shown.
Here is my code currently:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int seats[6][23], ticket_count, h, l;
char column;
column = 'A';
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 23; j++){
seats[i][j] = 0;
}
}
for (int i = 0; i < 6; i++) {
cout << column << " ";
column++;
for (int j = 0; j < 23; j++){
cout << seats[i][j];
cout << " ";
}
cout << endl;
}
cout << "\nEnter number of tickets to purchase: ";
cin >> ticket_count;
if (ticket_count > 23)
{
cout << "You can only purchase a maximum of 23 tickets.\n";
}
else if (ticket_count < 1)
{
cout << "You have to buy a minimum of 1 ticket.\n";
}
else
{
for (int q = 0; q < ticket_count; q++)
{
cout << "Ticket " << q + 1 << endl;
cout << "Row (1,2,3,4,5,6): ";
cin >> h;
cout << "Seat (1-23): ";
cin >> l;
h = h - 1;
l = l - 1;
seats[h][l] = 1;
column = 'A';
}
for (int i = 0; i < 6; i++) {
cout << column << " ";
column++;
for (int j = 0; j < 23; j++){
cout << seats[i][j];
cout << " ";
}
cout << endl;
}
}
ofstream myfile;
myfile.open("seats.txt");
myfile << seats;
myfile.close();
return 0;
}`
However, when I open the text file, it gives me a random hexadecimal string. I could really use some assistance. Thank you!

Coding hollow square in c++ with nested for loop and if statement

So for a lab project the goal is to print out shapes using a "*."
The four basic shapes are a square, two downwards pointing right triangles, and a hollow square. Everything seems to be working fine for any integer I input but I'm not entirely sure I'm doing the loop for the hollow square correctly and would like it if someone could give me some advice.
#include <iostream>
using std::cout; using std::endl; using std::cin;
int main()
{
cout << "Input arbitrary integer ";
int n;
cin >> n;
cout << endl;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = n; j >= i; j--)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = 2; j <= i; j++)
{
cout << " ";
}
for (int j = n; j >= i; j--)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++)
{
if (i == 1)
cout << "*";
else if (j == 1)
cout << "*";
else if (i == n)
cout << "*";
else if (j == n)
cout << "*";
else
cout << " ";
}
cout << endl;
}
}

How to draw a rectangle for c++ with loops?

So i have this code which makes a box, but want to make the corners +, the lengths |, and the widths - . Also want to input a number so you can draw them like cout<<"enter the length number" etc... how would i do that?
Here is what i have to make a box:
#include <iostream.h>
#include <string.h>
void main()
{
for(int z=1; z<=79; z++)
{
cout << "";
}
cout << endl;
for(int i=1; i<=5; i++)
{
cout << "";
for(int j=1; j<=77; j++)
{
cout << " ";
}
cout << "" << endl;
}
for(int y=1; y<=79; y++)
{
cout << "";
}
cout << endl;
}
Draws a rectangle where int height is the height and int width is the width
#include <iostream>
void draw_rect(int width,int height)
{
using std::cout;
cout << "+";
for (int i = 0; i < width - 2; i++)
{
cout << "-";
}
cout << "+\n";
for (int i = 0; i < height - 2; i++)
{
cout << "|";
for (int j = 0; j < width - 2; j++)
{
cout << " ";
}
cout << "|\n";
}
cout << "+";
for (int i = 0; i < width - 2; i++)
{
cout << "-";
}
cout << "+\n";
}
int main ()
{
draw_rect(8,6);
return 0;
}
And for how to get user input read this:
Basic C++ IO
#include <iostream>
using namespace std;
void draw_rect( int width, int height)
{
int i;
cout << char(218);
for (i=0; i<width-2; i++)
cout << char(196);
cout << char(191) << endl;
for (i=0; i<height-2; i++)
{
cout << char(179);
for (int j=0; j<width-2; j++)
cout << " ";
cout << char(179) << endl;
}
cout << char(192);
for(i=0; i<width-2; i++)
cout << char(196);
cout << char(217) << endl;
}
int main()
{
draw_rect(20,10);
return 0;
}