I have a program with bool function issquare, which code don't need for you for my question. I have iterative variable T, which is response for how many times I will put squarsized's time string. But after first input of strings programs stop, at T=1, and don't go to next iteration. Why?
int main(){
int T;
std::string line;
std::cin>>T;
int squaresize;
int** arr=new int*[30];
for(int d=0; d<30; d++){
arr[d]=new int[30];
}
for(int i=0; i<T; i++){
for (int d=0; d<30; d++){
for(int d1=0; d<30; d++){
arr[d][d1]=0;
}
}
std::cin>>squaresize;
for(int j=0; i<squaresize; i++){
std::cin>>line;
for(int a=0; a<squaresize; a++){
if (line[a]=='#'){
arr[j][a]=1;
}
}
}
if (issquare(arr, squaresize)==true){
std::cout<<"Case #"<<i+1<<": YES";
}
else{
std::cout<<"Case #"<<i+1<<": NO";
}
std::cout<<T;
}
return 0;
}
Instead of
std::cin>>line;
Try
getline(std::cin, line);
operator>> doesn't read an entire line, only until the first whitespace.
Instead of j you are comparing and incrementing i, which is also used by the outer loop:
for(int j=0; i<squaresize; i++){
std::cin>>line;
for(int a=0; a<squaresize; a++){
if (line[a]=='#'){
arr[j][a]=1;
}
}
}
In the future (and with possibly more complex programs), learning how to use of a debugger can really help you to locate such bugs. Your problem was that the outmost loop executed less than T times:
for(int i=0; i<T; i++){
Since the value of T is constant, something must be modifying i inside the loop. An easy way to debug this would be using a debugger to find out where the variable is changed. In Visual Studio this can be done by breaking and adding a data breakpoint from Debug -> New Breakpoint -> New Data Breakpoint -> Address: &i
Related
So Im new to coding, and I was wondering why my for loop is not going through argv correctly. When I run it, it doesnt run at all. Here is the bit of code that matters:
int main(int argc, char* argv[]){
bool isCapital[500];
bool capital;
bool isSpace[500];
bool space;
bool palindrome;
cout<<"Test";
for(int j=1; argv[j][0]!='-'; j++){
cout<<argv[j][0];
if('-'==argv[j][0]){
cout<<j;
for(int i=0; argv[j][i]!='\0'; i++){
if((argv[j][i])==('c'|'C')){
isCapital[j]=true;
break;
cout<<isCapital[j];
}
}
for(int i=0; argv[j][i]!='\0'; i++){
if((argv[j][i]==('s'|'S'))){
isSpace[j]=true;
break;
}
}
}
}
There are a couple of bugs in your code but I think the code below is major one:
for(int j=1; argv[j][0]!='-'; j++){
cout<<argv[j][0];
if('-'==argv[j][0]){
so let us look at it in more details.
for(int j=1; argv[j][0]!='-'; j++){
^^^^^^^^^^^^^^^
This part says: only execute the body of the for-loop if argv[j][0] is not a -
This line:
if('-'==argv[j][0]){
says: only execute the body of the if-statement if argv[j][0] is a -
These two are contradictions so execution never enters the body of the if-statement
Maybe you want this line
for(int j=1; argv[j][0]!='-'; j++){
to be
for(int j=1; j < argc; j++){
BTW:
As written in a comment by #swordfish
1) Notice that
if((argv[j][i])==('c'|'C'))
is probably not what you want. You probably want:
if(argv[j][i] == 'c' || argv[j][i] == 'C')
2) Notice that your loop condition should consider argc
3) That isCapital and isSpace should be initialized to "all false"
I am novice in vectors, I am tryind to input this 2D vector in main() function, but unable to do so.
int main()
{
int t, x, n;
cin>>n;
vector< vector <int> > jail(n);
for(int i=0; i<n; i++){
jail[i].reserve(n);
for(int j=0; j<n; j++){
cin>>jail[i][j];
}
}
cout<< jailBreak(jail,n-1,0,0)<<endl;
}
Runtime error is that I need to input an garbage input in the beginning of the program.
This ambiguous input has been bothering me for a long time now, thanx in advance for any advice on this.!
this line:
jail[i].reserve(n);
just tells vector to pre-allocate memory (it's just a hint to optimize further reallocs on push_back operations but does not guarantee allocation). You have to use resize instead which really allocates memory.
In your code:
for(int i=0; i<n; i++){
jail[i].reserve(n);
for(int j=0; j<n; j++){
cin>>jail[i][j];
}
}
jail[i].reserve(n);
should be jail[i].resize(n)
cin>>jail[i][j]
Never seen that work before. cin in to a temporary and then push.
int temp;
std::cin >> temp;
jail[i].emplace_back(temp);
I am trying to insert random numbers [1;100] into this array of 5 elements but so far I've been using only cin>> for inserting, when the user had to put something in. In this case I would like the random function to insert numbers, no user involved.
I show the code below because I am a beginner and the included pieces of codes are mostly what I can use and understand but on the other hand the whole code is useless because I don't know what is to connect with what. Where is rand()%100+1 to put?
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(0));
int a[5];
int r=rand()%100+1;
for(int i=0; i<5; i++)
{
cout<<a[i]<<endl;
}
for(int i=0; i<5; i++)
{
cout<<a[i]<<endl;
}
return 0;
}
In your program you have 2 loops that both print out result. I suppose previously you had one loop that used cin to insert values, like this:
for(int i=0; i<5; i++)
{
cin<<a[i]<<endl;
}
So you want to change from inserting with ci to inserting random number:
for(int i=0; i<5; i++)
{
a[i] = rand()%100+1
}
In your code you are not initializing the values of the array elements using the rand() function.
Your rand function should go inside the first 'for' loop.Something like the following will do the trick.
for (int i = 0; i < 5; i++) {
a[i] = rand()%100+1;
}
I have a problem programming in C++, I want to create and delete a triple pointer with dimension [3N][N][3], and I keep getting seg-fault without knowing in which line the error is...(N is an integer, N=2 to start)
#include <stdio.h>
#include <cmath>
#include <stdlib.h>
using namespace std;
int N=2;
int main(void){
double ***individuo;
individuo=new double**[3*N];
for(int k=0; k<3*N; ++k){
individuo[k]=new double*[3];
for(int i=0; i<N; ++i){
individuo[k][i]=new double[N];
}
}
for(int k=0; k<3*N; ++k){
for(int i=0; i<N; ++i){
delete []individuo[k][i];
}
delete []individuo[k];
}
delete []individuo;
return 0;
}
Looks like your first set of for loops is setting up the array with dimensions [3N][3][N], and then your second set is calling them as if it was dimensioned as [3N][N][]. This means that you aren't correctly accounting for all your allocated memory in the delete process.
You can use a debugger to find out where your code is giving the segfault. Or if you don't have one it's often useful to put in some unique print statements at various stages to try and find where it is giving the fault. Although make sure that the print statements are flushed from the buffer, most easily done using std::endl.
This
for(int k=0; k<3*N; ++k){
individuo[k]=new double*[3];
for(int i=0; i<N; ++i){
individuo[k][i]=new double[N];
}
}
should instead be this
for(int k=0; k<3*N; ++k){
individuo[k]=new double*[N];
for(int i=0; i<N; ++i){
individuo[k][i]=new double[3];
}
}
I have an assignment in which i have to create a console program in c++ that draws Hexagons in a given style. The problem i am having is; my For loops are never entered and i can't figure out why. here's the snippet of code I'm having trouble with.
void display()
{
int counter=0;//var that keeps track of the layer that is being drawn
for(int i=0;i>=size;i++)//spaces before first layer of hexagon
{
cout<<" ";
}
for (int k=0; k>size;k++)//top layer of hexagon
{
cout<<"_";
}
cout<<endl;//ends the first layer
for (counter; counter>=size-1;counter++)//outer loop for the top half that controls the size
{
for( int j=0;j>(size-counter);j++)//adds spaces before the shape
{
cout<<" ";
}
cout<<"/";
for( int p=0; p>(size+(counter*2));p++)//loop for the hexagon fill
{
cout<<fill;
}
cout<<"\\"<<endl;
}
for(counter;counter==0;counter--); //loop for bottom half of the hexagon
{
for( int j=0;j>(size-counter);j++)//adds spaces before the shape
{
cout<<" ";
}
cout<<"\\";
for( int p=0; p>(size+(counter*2));p++)//loop for the hexagon fill
{
cout<<fill;
}
cout<<"/"<<endl;
}
cout<<"\\";
for(int r=0; r>=size;r++){cout<<"_";}
cout<<"/"<<endl;
}
the 'Size' and 'fill' are selceted earlier in the program during my main()
I'm probably missing something very simple but I've been struggling with this for a while. Any help would be greatly appricated!
Your loops use > and start at 0. It seems you want < instead. For example
for(int i=0;i<size;i++)//spaces before first layer of hexagon
{
cout<<" ";
}
I am not sure what is the content of your size variable but it looks like you have got your loop conditions wrong:
for(int i=0;i>=size;i++)
probably should be:
for(int i=0;i<size;i++)
The same goes for other loops.
Assuming your size is a postive number, it works as per your condition.
Change the > conditojs to < in your conditions.
In your conditions, invert the > to <
< means inferior, you want to do a
for i = 0; if i < size; i++
You do
for i = 0 ; if i > size ; i ++
if size is superior to i (0) the loop will never trigger
Aren't all of your < and > reversed? Because
(int k=0; k>size;k++)
makes no sense to me.
for loops in C++ are while loops, not until loops.
C++ has only while loops (with the meaning of as long as):
for (int i=0; i<10; ++i)
....
int i=0;
while (i<10) {
....
++i;
}