C++ program hangs when setting variable - c++

I'm writing a program in C++ and I have input with bounds and a grid for example:
4 4
SOUI
1234
WER5
0234
I'm using the following loop to read input
int N, M, startR = 0, startC = 0;
string matrix[105][105], instr;
cin >> N >> M;
for(int i = 0; i < N; i++){
cin >> instr;
for(int j = 0; j < M; j++){
matrix[i][j] = instr[j];
if(matrix[i][j] == "B"){
startR = i;
startC = j;
}
}
}
If I use this code, the program will ask for more input and won't run no matter what I give it. This means the code after this input loop is never used. However, if I remove the setting of variables, the program runs fine. For example:
int N, M, startR = 0, startC = 0;
string matrix[105][105], instr;
cin >> N >> M;
for(int i = 0; i < N; i++){
cin >> instr;
for(int j = 0; j < M; j++){
matrix[i][j] = instr[j];
}
}
I'm not sure why this is happening as all I'm doing is setting some other variables. I need the information about where the variable B is found. Any ideas why this is happening?

I solved this as the problem lied in the code after the loop. The problem was misleading as it seemed like cin was taking more input but it was actually the infinite loop later. I tried to first debug using print statements immediately after the cin statement to see what was being taken in but those weren't run which is what threw me off. My takeaway is to look at the program completely and use a full debugger.

Related

For some reason, when i use getch() my program crash, but if i use cin, then it works

I would like to know what knowledge I lack about inputs of arrays. I want to input one character and then automatically go to the next line of the code.
Here is the code:
char word[21];
for(int i = 0;i < 21; i++)
{
word[i] = getch();
//cin>>word[i];
if(word[i] == '/')break;
}
for(int j = 0;j < strlen(word); j++ )
{
if(j < (strlen(word) - 1 ))cout<<word[j];
}
Here's how I would do this:
char c;
std::cin >> c; // Input the character.
std::cin.ignore(10000, '\n'); // Ignore remaining characters on the line.
You could replace 10000 with the maximum value for unsigned integers, but I just use an improbable large number (to fuel the improbability drive).

Why does my C++ code for a horizontal bar graph not allow user input?

Here is my code. This is done in c++
#include <iostream>
using namespace std;
int main ()
{
int input [1];
int n = 0;
for (int i = 0; i < 1; i++)
{
cout << "usage: bargraph integer-list \n"; /*This is the only thing showing up in my terminal*/
cin >> n;
}
for (int b=0; b < 1; b++)
{
cout << b;
for (int j=0; j<input[j]; j++)
{
cout << "#";
}
cout << "/n";
}
return 0;
}
I would like this to be able to enter in as many user inputs as the user wants.
int input [1];
Here you apparently want to allow more than 1 item to be entered, but your input array can only hold one value. One way to support an arbitrary number of values would be to use an std::vector<int> instead of an array of int. You can use push_back to add items to the vector.
cout << "usage: bargraph integer-list \n";
This has a fundamental problem: if you enter the integers on the command line, your program will see them as elements of argv, which is passed as a parameter to main. Data you read from standard input has to be entered separately (or the user can use command-line redirection to read standard input from a file of their choice). Either one can/will work fine, but you need to write the code to support the one you choose, and the user is almost certain to get confused if you tell them to do one, but actually support the other.
for (int b=0; b < 1; b++)
{
cout << b;
for (int j=0; j<input[j]; j++)
I'd guess you intended:
int j=0; j<input[b]; j++)
// ---------------^
You used j where you almost certainly intended to use b.
One way to prevent problems like that is to use more meaningful variable names. If you used something like current_line and line_length instead of b and j:
for (int current_line = 0; current_line < count; current_line++)
for (int line_length = 0; line_length < input[line_length]; line_length++)
...the problem would probably be at least somewhat more apparent.

Problems with for loop c++

i used to code in javascript, but my new school force me to learn c++.
Im kind new in this language, and here's the problem:
In javascript i can write such a code:
for(let i = 0; i < 10; i++){
var variable[i] = i+3;
}
for(let j = 0; j < 10; j++){
console.log(variable[j]);
}
You may say 'Why don't you just write the code into 1 for loop', but that's only example.
And now i'm trying to rewrite above code to cpp:
int n,k,w;
cin>>n>>k;
for(int i = 0; i < n; i++){
int w[i];
cin>>w[i];
}
//some code here
for(int i = 0; i < n; i++){
cout<<w[i];
}
And here's the question. How can i cout all variables w with index i, cause im getting an error [Error] invalid types 'int[int]' for array subscript.
What you probably want is:
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec;
int size = 0;
std::cin >> size;
for(int i = 0; i < size; i++){
int number = 0;
std::cin >> number;
vec.push_back(number);
}
for(int i : vec){
std::cout << i << " ";
}
}
std::vector<int> is a class designed to provide an interface to resizable array. The push_back() function appends the vector with given argument.
The last loop, which is called a ranged-based for(), is used to print all elements of the vector. You can replace it with plain old for() loop with indexing, since std::vector supports operator [], but if ranged-based approach is sufficient, it should be preferred.
EDIT: I don't know JavaScript, but I assume (from your example) that variables declared inside loops are visible everywhere. This is not the case in C++. Variables' visibility is dependent on the scope they are declared in. If you want your list/array/vector/any other container to be visible to those two for() loops, you have to declare it outside them - like in my example above.
EDIT2: While you should almost always use std::vector for such tasks, one could argue that they want to disable resizing the container. In this case, we are left with simple dynamic allocation. We reach for <memory> library, since we shouldn't manage it ourselves:
#include <iostream>
#include <memory>
int main() {
int size = 0;
std::cin >> size;
auto arr = std::make_unique<int[]>(size);
for(int i = 0; i < size; i++){
int number = 0;
std::cin >> number;
arr[i] = number;
}
for(int i = 0; i < size; i++){
std::cout << arr[i] << " ";
}
}
For auto, either read here or imagine that it's just a magic type that is (almost always) correct. It's like var in Python or JavaScript (but later on its type cannot be changed).
For std::unique_ptr<T[]>, either read here or imagine that it's just a dynamically allocated array that automatically delete[]s itself. If you did not learn about dynamic allocation yet, simply ignore what that means and wait until it's introduced.
Notice that we also got rid of the ranged-based for() loop. Unfortunately, it does not work with plain, dynamically allocated arrays.
Not all compilers support VLA so stick to the Standards; always specify a constant size for arrays. If you need some dynamically changed size then consider using another type of containers like: std::vector.
Also why you re-declared int w inside the for loop? It is local to for loop and changes to it won't affect the outer one.
You get a compile time error in the second loop that complains that w is not an array. To solve it make int w[] outer and on top of the two loops.
int n, k;
cin >> n >> k;
int w[n];
for(int i = 0; i < n; i++)
cin >> w[i];
//some code here
for(int i = 0; i < n; i++)
cout << w[i];
The alternative to VAL use std::vector:
std::vector<int> w;
int n, k;
std::cin >> n;
for(int i(0); i!= n; ++i){
std::cin >> k;
w.push_back(k);
}
for(auto i(0); i != w.length(); ++i)
std::cout << w[i] << ", ";

Getting WA for SPOJ smpseq

Here is the link to the question: http://www.spoj.com/problems/SMPSEQ3/
I am getting WA every time although the code works for all of the test cases I have tried
Please give me a hint.I am a beginner in this
This is my code.
#include<iostream>
using namespace std;
int main()
{
int n, m;
bool check;
int cnt=0;
cin >> n;
int s[n];
int c[n];
for(int i = 0; i < n; i++)
cin >> s[i];
cin >> m;
int q[m];
for(int i = 0; i < m; i++)
cin >> q[i];
for(int i = 0; i < n; i++)
{
check = false;
int j = 0;
while(q[j] <= s[i])
{
if(q[j] == s[i])
check = true;
j++;
}
if(check == false)
{
c[cnt] = s[i];
cnt++;
}
}
for(int i = 0; i < cnt; i++)
cout << c[i] << " ";
return 0;
}
You code fails because you do not keep a check on the value of j whether it is less than m or not, you simply keep on incrementing it until you hit a value where q[j] <= s[i]. So, when your j becomes m in the while loop, you are actually accessing memory which was not allocated to you, and there is some garbage value stored there. Since the garbage value can be anything you might eventually miss some values in q[] which should be added to c[].
So, your while condition shall look like this ::
while(j < m && q[j] <= s[i])
I think this shall give you an AC.
A few more things you initialize j = 0 every time you enter the for loop, which is useless. Since in the question it is given that both the sequences are sorted, so considering the sequences ::
S = a, b, c, d . . .
Q = aa, bb, cc, dd . . .
So, considering if aa < a but bb > a so, in the first iteration your j stops at bb. So, now when i comes at b in S, since a > aa and b >= a(S is a sorted sequence), so b > aa as well. So, you do not need to initialize j = 0 at the beginning of every iteration, which will save you wasteful computations. You just need to initialize j = 0 at beginning of the main.
Moreover, you do not need to save the result in a new array, you can simply print it, when you encounter check == false, this save you some space.

Reading data from file into array

I am trying to read specific data from a file into two 2D arrays. The first line of data defines the size of each array so when I fill the first Array i need to skip that line. After skipping the first line, the first array fills with data from the file until the 7th line in the file. The second array is filled with the rest of the data from the file.
Here's a labeled image of my data file:
and here's my (flawed) code so far:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream inFile;
int FC_Row, FC_Col, EconRow, EconCol, seat;
inFile.open("Airplane.txt");
inFile >> FC_Row >> FC_Col >> EconRow >> EconCol;
int firstClass[FC_Row][FC_Col];
int economyClass[EconRow][EconCol];
// thanks junjanes
for (int a = 0; a < FC_Row; a++)
for (int b = 0; b < FC_Col; b++)
inFile >> firstClass[a][b] ;
for (int c = 0; c < EconRow; c++)
for (int d = 0; d < EconCol; d++)
inFile >> economyClass[c][d] ;
system("PAUSE");
return EXIT_SUCCESS;
}
Thanks for the input everyone.
Your while loops iterate until the end of file, you don't need them.
while (inFile >> seat) // This reads until the end of the plane.
Use instead (without the while):
for (int a = 0; a < FC_Row; a++) // Read this amount of rows.
for (int b = 0; b < FC_Col; b++) // Read this amount of columns.
inFile >> firstClass[a][b] ; // Reading the next seat here.
Apply the same for economic seats.
Also you might want change arrays into vectors, since variable size arrays are hell.
vector<vector<int> > firstClass(FC_Row, vector<int>(FC_Col)) ;
vector<vector<int> > economyClass(EconRow, vector<int>(EconCol)) ;
You need to #include <vector> to use vectors, their access is identical to arrays.
You need to change the order of the for loops and reading from the file:
for (rows = 0; rows < total_rows; ++ rows)
{
for (col = 0; columns < total_columns; ++cols)
{
input_file >> Economy_Seats[row][column];
}
}
I'll leave checking for EOF and handling of invalid input to the reader.
You're reading into seat once then filling the array with this value. Then you're reading into seat again, and filling the entire array with this new value.
Try this:
int CurRow = 0;
int CurCol = 0;
while ( (inFile >> seat) && (CurRow < FC_Row)) {
firstClass[CurRow][CurCol] = seat;
++CurCol;
if (CurCol == FC_Col) {
++CurRow;
CurCol = 0;
}
}
if (CurRow != FC_Row) {
// Didn't finish reading, inFile >> seat must have failed.
}
Your second loop should use economyClass not firstClass
The reason for switching the loop around like this is error handling, which is simplified when the loop exits upon error. Alternatively you could keep the for loops, use infile >> seat in the inner loop, but you'd then have to break out of two loops if reading failed.