Please help Debug this program for me

#include <cstdint> //To get size info for INT.
#include <cfloat> //To get size in for float
#include<iostream>

using namespace std;

int main(int argc, char *argv[])
{
cout<<"Number of bits in a nibble:"<<CHAR_BIT/2<<endl; //How many bits in a byte.
cout<<"Number of bits in a byte:"<<CHAR_BIT<<endl; //How many bits in a byte.

//The size of stuff
cout<<"How may bytes in an int: "<<sizeof(int)<<endl;
//4 bytes in an int
// For every 1 byte we have 8 bits
//4 sets of 8 bits
//[00000000 00000000 00000000 00000000]

cout<<"The maximum size in decimal that int can be "<<INT_MAX<<endl;

cout<<"How may bytes in an float: "<<sizeof(float)<<endl;
cout<<"How may bytes in an double: "<<sizeof(double)<<endl;

cout<<"The maximum size in decimal that float can be "<<FLT_MAX<<endl;

cout<<"How may bytes in an char: "<<sizeof(char)<<endl;

cout<<"How may bytes in an bool: "<<sizeof(bool)<<endl;

//static allocation
int nums[500]={};

//dynamic
//Change the size as you eed still in code but while is running
//new delete
int *nums2 = new int[2];

nums2[0] = 40;
nums2[1]=20;

cout<<*nums2<<endl;

nums2++;

cout<<*nums2<<endl;

//Start with dynamic array of size 2.
//Ask the user to start entering values.
//When the user enteres more then 2 values increase the array size by 2
// int lnth, capacity;

// capacity = 8;

// char *c = new char[capacity]; //8 items //[A,B,C,D,E,F,G,H] <- I J K

//Lets resize the array
//char* temp;

// char *temp = new char[2*capacity]; //16 items []

// for (int i=0; i<capacity; i++) {
// temp[i] = c[i]; //[A,B,C,D,E,F,G,H, , , , , , , , ]
// }

// capacity = capacity*2;

// delete [] c;

// c = temp;

//Final solution
int capacity = 5;
int numsAdded =0;
int *numList = new int[capacity];

while(true)
{
int userNum =0;

cout<<"Enter list of numbers. One number at a time. Enter 0 to stop"<<endl;
cin>>userNum;

if(userNum==0)
{
break;
}

if(capacity>numsAdded)
{
*numList = userNum;
numList++;
numsAdded++;
}
else
{
// char *temp = new char[2*capacity]; //16 items []

// for (int i=0; i<capacity; i++) {
// temp[i] = c[i]; //[A,B,C,D,E,F,G,H, , , , , , , , ]
// }
}


}

//After everything print the list
for(int a =0;a<capacity;a++)
{
cout<<numList[a]<<endl;
}


return 0;
}
Welcome! If you want help, help us a bit.
put it in code tags <> on the editor. you can edit your post to fix this, just select all and click the button.
and tell us what bug you are seeing or what it should do vs what it does.

int *nums2 = new int[2];
nums2[0] = 40;
nums2[1]=20;
cout<<*nums2<<endl;
nums2++; //this is probably not good. you can get back with a --, but you are in danger of a leak or access violation if you are not careful. you can't delete until you -- and get back to the true location.

you need 1 delete for every call to new.
your final section looks unfinished, like you should resize, but then did not? unclear.
Last edited on
Topic archived. No new replies allowed.