Trying to write a file that reads from the first byte in a file to a byte specified by the user. I need help on the logic. If the file just has letters a through z and I just want to read and display the first 10, for example. here's a piece of what I wrote:
char byte;
inFile.seekg(0L,ios::beg);
inFile.get(byte);
cout << byte;
for(int i = 0; i < num; i++); //num is an int specified by the user.
{
inFile.seekg(1, ios::cur);
inFile.get(byte);
cout << byte;
}
First problem - the semi-colon on the end of the for() line:
for(int i = 0; i < num; i++);
{
...
}
What the compiler sees is actually this:
for(int i = 0; i < num; i++) { /* do nothing num times */ }
{
// code that will only run once
}
So, remove the semi-colon.
Next, if you're just reading bytes in succession, there's no need to seek in between each one. Just calling get() will get the next byte in the sequence. So remove the seekg() calls.
Final problem - this function is actually calling inFile.get() a total of num + 1 times. The first call is before the for loop. Then in the for loop, get() is called num times (ie. i = 0, 1, 2, 3 ... num-1). You can fix this either by changing the for loop counter to (i = 1; i < num; i++) or just removing the get() call before the for loop. In the code below I've chosen the second way:
void run(int num, istream &inFile)
{
char byte;
for(int i = 0; i < num; i++)
{
inFile.get(byte);
cout << byte;
}
}
Related
c++
When printing to console, if function execution is sequential it would seem logical the ordered array would be printed after calling insertionSort, however order list does not print until next loop. Any help would be appreciated.
#include <stdio.h>
#include <iostream>
#include <array>
using namespace std;
void insertionSort(int* array, int size) {
for (int i = 1; i < size; i++) {
int key = i - 1;
while (i > 0 && array[key] > array[i] ) {
int tmp = array[i];
array[i] = array[key];
array[key] = tmp;
i -= 1;
key -= 1;
}
}
}
const int ARRAY_MAXSIZE = 5;
int main(void) {
int *array = (int*)calloc(ARRAY_MAXSIZE, sizeof(int));
int input;
cout << "Enter 5 digits\n";
for (int size=0; size < ARRAY_MAXSIZE; size++) {
cout << size << " index ";
cin >> input;
array[size] = input;
insertionSort(array, size);
for (int j=0; j <= size; j++) {
cout << array[j];
}
cout << '\n';
}
}
Console Entry
This is a classic off-by-one error. Your insertionSort expects you to pass the number of elements to sort via the parameter size. But your main loop is always holding a value that is one less than the size immediately after adding an element.
I want to say that bugs like this are easily discovered by stepping through your program's execution with a debugger. If you don't know how to use a debugger, start learning now. It is one of the most important tools used by developers.
Anyway, the quick fix is to change your function call to:
insertionSort(array, size + 1);
However, as Paul McKenzie pointed out in comments, it's a bit crazy to do this every time you add a new element because your function sorts an entire unsorted array. Your array is always nearly sorted except for the last element. You only need to call that function once after your input loop is done:
// Read unsorted data
for (int size = 0; size < ARRAY_MAXSIZE; size++) {
cout << size << " index ";
cin >> input;
array[size] = input;
}
// Sort everything
insertionSort(array, ARRAY_MAXSIZE);
// Output
for (int j = 0; j < ARRAY_MAXSIZE; j++) {
cout << array[j];
}
cout << '\n';
But if you want every insertion to result in a sorted array, you can "slide" each new value into place after inserting it. It's similar to a single iteration of your insertion-sort:
// Sort the last element into the correct position
for (int i = size; i >= 1 && array[i] > array[i - 1]; i--)
{
std::swap(array[i], array[i - 1]);
}
Even better, you don't need to swap all those values. You simply read the value, then shuffle the array contents over to make room, then stick it in the right spot:
// Read next value
cin >> input;
// Shuffle elements to make room for new value
int newPos = size;
while (newPos > 0 && array[newPos - 1] > input) {
array[newPos] - array[newPos - 1];
newPos--;
}
// Add the new value
array[newPos] = input;
I have an array that reads data from a file, the data is binary digits such as 010011001001 and many others so the data are strings which I read in to my 2d array but I am stuck on comparing each value of the array to 0. Any help would be appreciated.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string myArr[5000][12];
int i = 0, zeroCount = 0, oneCount = 0;
ifstream inFile;
inFile.open("Day3.txt");
while(!inFile.eof())
{
for(int i = 0; i < 5000; i++)
{
for(int j = 0; j < 12; j++)
{
inFile >> myArr[i][j];
j++;
}
i++;
}
}
for(int j = 0; j < 12; j++)
{
for(int i = 0; i < 5000; i++)
{
if(myArr[i][j].compare("0") == 0)
{
zeroCount++;
}
else
{
oneCount++;
}
i++;
}
if(zeroCount > oneCount)
{
cout << "Gamma is zero for column " << i << endl;
}
else
{
cout << "Gamma is One for column " << i << endl;
}
j++;
}
}
some input from the text file:
010110011101
101100111000
100100000011
111000010001
001100010011
010000111100
Thank you for editing you question and providing more information. Now, we can help you. You have 2 major misunderstandings.
How does a for loop work?
What is a std::string in C++
Let us start with the for loop. You find an explanation in the CPP reference here. Or, you could look also at the tutorial shown here.
The for loop has basically 3 parts: for (part1; part2; part3). All are optional, you can use them, but no need to use them.
part1 is the init-statement. Here you can declare/define/initialize a variable. In your case it is int i = 0. You define a variable of data type int and initialize it with a value of 0
part2 is the condition. The loop will run, until the condition becomes false. The condition will be check at the beginning of the loop.
part3 is the so called iteration-expression. The term is a little bit misguiding. It is basically a statement that is executed at the end of the loop, before the next loop run will be executed and before the condition is checked again.
In Pseudo code it is something like this:
{
init-statement
while ( condition ) {
statement
iteration-expression ;
}
}
which means for the part of your code for(int j = 0; j < 12; j++)
{
int j = 0; // init-statement
while ( j < 12 ) { // while ( condition ) {
inFile >> myArr[i][j]; // Your loop statements
j++; // Your loop statements PROBLEM
j++; // iteration-expression from the for loop
}
}
And now you see the problem. You unfortunately increment 'j' twice. You do not need to do that. The last part3 of the for loop does this for you already.
So please delete the duplicated increment statements.
Next, the std::string
A string is, as its names says, a string of characters, or in the context of programming languages, an array of characters.
In C we used to write actually char[42] = "abc";. So using really a array of characters. The problem was always the fixed length of such a string. Here for example 42. In such an array you could store only 41 characters. If the string would be longer, then it could not work.
The inventors of C++ solved this problem. They created a dynamic character array, an array that can grow, if needed. They called this thing std::string. It does not have a predefined length. It will grow as needed.
Therefore, writing string myArr[5000][12]; shows that you did not fully understand this concept. You do not need [12], becuase the string can hold the 12 characters already. So, you can delete it. They characters will implicitely be there. And if you write inFile >> myString then the extractor operator >> will read characters from the stream until the next space and then store it in your myString variable, regardless how long the string is.
Please read this tutorial about strings.
That is a big advantage over the C-Style strings.
Then your code could look like:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string myArr[5000];
int zeroCount = 0, oneCount = 0;
ifstream inFile;
inFile.open("Day3.txt");
while (!inFile.eof())
{
for (int i = 0; i < 5000; i++)
{
inFile >> myArr[i];
}
}
for (int i = 0; i < 5000; i++)
{
zeroCount = 0; oneCount = 0;
for (int j = 0; j < 12; j++)
{
if (myArr[i][j]== '0')
{
zeroCount++;
}
else
{
oneCount++;
}
}
if (zeroCount > oneCount)
{
cout << "Gamma is zero for column " << i << endl;
}
else
{
cout << "Gamma is One for column " << i << endl;
}
}
}
But there is more. You use the magic number 5000 for your array of strings. This you do, because you think that 5000 is always big enough to hold all strings. But what, if not? If you have more than 5000 strings in your source file, then your code will crash.
Similar to the string problem for character arrays, we have also a array for any kind of data in C++, that can dynamically grow as needed. It is called std::vector and you can read about it here. A tutorial can be found here.
With that you can get rid of any C-Style array at all. But please continue to study the language C++ further and you will understand more and more.
Ther are more subtle problems in your code like while(!inFile.eof()), but this should be solved later.
I hope I could help
Basically, I'm reading a file and trying to store the data in a 2D, for the differentiation between rows and columns I use the logic below:
int rows=0,column=0;
char arr[50][50];
while(my_file.eof()==0){
my_file.get(ch);
if(ch=='\n'){
rows++;
}
arr[rows][column]=ch;
column++;
}
for(int j=0;j<rows;j++){
for(int k=0;k<column;k++){
cout<<arr[j][k];}
}
But the when I run It shows the following output: https://i.stack.imgur.com/XzhST.png
And the text file data is:
I am going to school
hi!
Hello
guide me a bit...
Hmm, a 2D char array can indeed be used to store an number of lines, but you should control that you never try to store more than 50 characters for a single line, and that you never try to ouput more characters for a line than what it initially contained.
Here is a minimal fix of your code:
int rows = 0, column = 0;
char arr[50][50] = { {0 } }; // ensure the array is initialized with '\0' chars
for (;;) {
my_file.get(ch);
if (!my_file) break; // eof shall be tested AFTER a read operation
if (ch == '\n') {
rows++;
if (rows == 50) break; // no more than 50 lines
column = 0; // reset column index for next line
}
else if (column < 50) { // no more than 50 columns
arr[rows][column] = ch;
column++;
}
}
for (int j = 0; j < rows; j++) {
for (int k = 0; k < 50; k++) {
if (arr[j][k] == 0) break; // stop on end of line
std::cout << arr[j][k];
}
std::cout << '\n'; // and display the end of line
}
And as you have been said this is rather C-ish... I assume it is only for learning how 2D arrays can work.
As pointed out in comments, you'd be much better off using a std::vectorstd::string to store the strings.
But, this looks like a homework assignment to read then print each byte separately, so let's have a look... I'll add one of the ways this is usually done at the end of this post.
Your output looks like this:
It looks like you are displaying characters beyond the bondary of the strings, or that your strings are not null terminated... Turns out it's both.
Your code:
int rows = 0, column = 0;
char arr[50][50]; // <-- your array is not initialized, while that is not
// a big issue, filling the array with zeroes is easy:
// char arr[50][50] = {};
while (my_file.eof() == 0) {
my_file.get(ch);
if (ch == '\n') {
rows++; // <-- you pass to the next string, but do not put a
// null character to properly terminate your strings
// while this could have been avoided by initializing
// the array, it's best to do it explicitely.
// replace above line contents by:
arr[row][column] = '\0';
if (++row >= 50) // consider using named constants for the size of your array.
break; // No use keeping on reading strings if there is no
// more room to store them
}
arr[rows][column] = ch; // <-- I suspect a bunch un undefined stuff will
// start happening when column >= 50
column++;
// Try replacing above code with:
if (column < 50) // consider using named constants for the size of your array.
arr[rows][column++] = ch;
}
// make sure the last string is null terminated.
if (row < 50 && column < 50)
arr[row][column] = '\0';
// note that strings that are 50 bytes long are NOT null terminated.
// that's important to keep in mind, and only workss because we'll print
// byte by byte.
// your original print routine prints out all characters in the array, even
// stuff that was not in the original file...
for (int j = 0; j < rows; ++j){
for (int k=0 ; k < column; ++k){ // <-- you need to check for a null
// terminating character here...
// also, column is the length of the last
// string in the array. This is not a very
// useful value for displaying any other
// strings, is it?
// try this:
for (int k = 0; k < 50 && arr[j][k] != '\0'; ++k)
cout << arr[j][k];
}
cout << '\n'; // insert a newline after each string.
}
As you can tell, this is overly complex for doing a very common operation... Here's a more concise way of doing the same thing:
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
int main()
{
std::vector<std::string> arr;
std::ifstream ifs("testfile.txt");
while (ifs && !ifs.eof())
{
std::string str;
std::getline(ifs, str);
arr.push_back(str);
}
for (size_t i = 0; i < arr.size(); ++i)
std::cout << arr[i] << '\n';
return 0;
}
Because you haven't compile the array yet
char arr[50][50];
for (int r = 0; r < 50; r++){
for (int c = 0; c < 50; c++){
arr[r][c] = ' ';}
}
My goal is to solve this:
Given a string S, of length N that is indexed from 0 to N-1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line
int main() {
int test;
cin>>test;
for(int j = 0; j < test; j++){
char str[10000];
cin.ignore();
cin.getline(str,9999);
for( int i = 0; i < strlen(str); i++)
{
if(i % 2 == 0)
cout<<str[i];
}
cout <<" ";
for(int i = 0; i < strlen(str); i++)
{
if((i % 2 != 0))
cout<<str[i];
}
cout << endl;
}
return 0;
}
The above code gives me output as :
Hce akr
ak n
for the given input of:
2
Hacker
Rank
But when I use cin>>str instead of the cin.ignore() and cin.getline(), I get the correct expected output: How does that change the result?
Hce akr
Rn ak
You should Write cin.ignore(); before for loop . Your code ignoring first charecter of every string without first iteration. You need to ignore line break for only test not for every string .
see the below code :
int main() {
int test;
cin>>test;
cin.ignore();
for(int j = 0; j < test; j++){
char str[10000];
cin.getline(str,9999);
//cin>>str;
for( int i = 0; i < strlen(str); i++)
{
if(i % 2 == 0)
cout<<str[i];
}
cout <<" ";
for(int i = 0; i < strlen(str); i++)
{
if((i % 2 != 0))
cout<<str[i];
}
cout << endl;
}
return 0;
}
input :
5
Hacker
Rank
WoW
check
lastone
output :
Hce akr
Rn ak
WW o
cek hc
lsoe atn
Using ignore()+getline():
On the first loop iteration, cin.ignore() skips the line break that was left behind from cin>>test, then cin.getline() reads the entire line (Hacker) including but swallowing the line break.
On the second loop iteration, cin.ignore() skips the 1st character of the next line (R), and then cin.getline() reads the remaining characters of the same line (ank) including but swallowing the line break.
The solution is to move the call to cin.ignore() to above the loop:
cin>>test;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // <-- move here
for(int j = 0; j < test; j++){
char str[10000];
cin.getline(str,9999);
...
}
Using operator>>:
On the first loop iteration, cin>>str skips the line break that was left behind from cin>>test and then reads the next available word (Hacker).
On the second loop iteration, cin>>str skips the line break that was left behind from the previous cin>>str and then reads the next available word (Rank).
I'm trying to read a textfile that I've edited with Vim into an array.
The textfile is 30*50 and is composed of single digit numbers. I've been going crazy trying to get it to work, but I think I'm having issues due to newline characters. Here's what I've been using:
Map::Map(char* filename)
{
grid[30][50] = (0);
string line;
ifstream m_file(filename);
if (m_file.is_open())
{
while(m_file.good())
{
for (int i = 0; i < 30; i++)
{
getline(m_file,line);
for (int k = 0; k < 50; k++)
{
int tnum = atoi(line.c_str());
grid[i][k] = tnum;
}
}
}
m_file.close();
}
};
grid is defined in the header file as int grid[30][50].
The code I use to print is as follows:
void display_room(int trid[30][50])
{
for (int i = 0; i < 30; i++)
{
for (int k = 0; k < 50; k++)
{
mvprintw(i,k,"%d",trid[i][k]);
};
};
};
after calling Map sMap = Map("testmap");
I'm simply trying to capture the single digit numbers into an array, and reprint that array (using curses). Currently, it reads the testmap file, and prints all zeros, no matter what is in the testmap file.
If I understand Your problem: Your parsing sets the value from the entire line where only a digit should be...
int tnum = atoi(line.c_str());
grid[i][k] = tnum;
Translating the digit (ASCII to an int/byte/... can be done in this way:
grid[i][k] = line[k] - '0';
(Perhaps some casting is needed.)
In the inner loop, you're calling atoi with the full content of the line each time. As the line is 50 character long, atoi cannot convert it to an int (the largest representable value by an int is 2147483647, and your number is probably larger than that). When atoi fails, it return 0.
What you want is convert each character of the line into an int. Something like that:
for (int i = 0; i < 30; i++)
{
getline(m_file,line);
for (int k = 0; k < 50; k++)
{
// The ASCII character of the digits 0 to 9 have
// successives values.
int tnum = line[k] - '0';
grid[i][k] = tnum;
}
}
Look at your code again. Try to see what is actually says instead of what you hope it says
int tnum = atoi(line.c_str());
You clear want that line to read each of the fifty numbers on the line in turn. But it doesn't say that. It tries to turn the whole line into an integer (and tries to do that fifty times).
Since your numbers are single digits, you actually need something much simpler
int tnum = line[k] - '0';
By saying line[k] you will get a different digit each time round the loop (because k increases each time round the loop). The - '0' bit is just a trick to turn a character into an integer. See if you can work out how it works.