I am reading a file which has variable sized columns:
0 1 3 0
0 2 0
0 4 0
My code reads the file, but after the last "0" it outputs two numbers which are garbage. Output is: 0130020040-8457888-85648454 (something like that): Please Help me THANK YOU
int **routes = new int *[3]; //create route matrix
for(int i=0;i<3;i++)
{
routes[i] = new int[sizeof(routes)];
}
ifstream routefile;
routefile.open("Sweep_routes.txt");
if(routefile.fail()){
cout << "ERROR";
exit(1);}
for (int i=0;i<3;i++){
for (int j=0;j<sizeof(routes);j++){
routefile>>routes[i][j]; //read
}
}
for (int i=0;i<3;i++){
for (int j=0;j<sizeof(routes);j++){
cout << routes[i][j]; //display
}
}
system("PAUSE");
return 0;
}
You can use this alternative to your code:
#include <iostream>
#include <string>
#include <array>
#include <fstream>
int main()
{
std::array<std::array<std::string, 3>, 3> routes;
std::ifstream routefile("Sweep_routes.txt");
if (routefile)
{
for (auto x : routes)
{
for (auto y : x)
{
if (routefile >> y)
std::cout << y;
}
}
}
std::cin.get();
}
Related
I don't understand why CLion IDE underlines "pthread_create" and "pthread_join" in red and says "No matching function for call to...". I used a similar code without using the pointer to an object passed to the thread as an argument and it worked.
#include <iostream>
#include <pthread.h>
#define NUM_THREADS 4
using namespace std;
class Animal {
private:
float x, y;
public:
Animal(float x, float y) {
this->x = x;
this->y = y;
}
void print() {
cout<< x << "," << y << endl;
}
};
void *function(Animal *p) {
Animal animal = *p;
animal.print();
}
int main() {
pthread_t thread[NUM_THREADS];
Animal dog[] = {Animal(2, 3), Animal(-1, 2), Animal(5, 2), Animal(5, 10)};
for(int i = 0; i<NUM_THREADS; i++) {
pthread_create(&thread[i], NULL, function, &dog[i]);
}
for(int i = 0; i<NUM_THREADS; i++) {
pthread_join(thread[i]);
}
return 0;
}
I changed the argument of the childThread function from Animal to void *
void *childThread(void *p) {
Animal *animal = (Animal *)p;
animal->print();
}
And added a second argument NULL to pthread_join
pthread_join(thread, NULL)
and now it works
I am new to using condition_variables and unique_locks in C++. I am working on creating an event loop that polls two custom event-queues and a "boolean" (see integer acting as boolean), which can be acted upon by multiple sources.
I have a demo (below) that appears to work, which I would greatly appreciate if you can review and confirm if it follows the best practices for using unique_lock and condition_variables and any problems you foresee happening (race conditions, thread blocking, etc).
In ThreadSafeQueue::enqueue(...): are we unlocking twice by calling notify and having the unique_lock go out of scope?
In the method TheadSafeQueue::dequeueAll(): We assume it is being called by a method that has been notified (cond.notify), and therefore has been locked. Is there a better way to encapsulate this to keep the caller cleaner?
Do we need to make our class members volatile similar to this?
Is there a better way to mockup our situation that allows us to test if we've correctly implemented the locks? Perhaps without the sleep statements and automating the checking process?
ThreadSafeQueue.h:
#include <condition_variable>
#include <cstdint>
#include <iostream>
#include <mutex>
#include <vector>
template <class T>
class ThreadSafeQueue {
public:
ThreadSafeQueue(std::condition_variable* cond, std::mutex* unvrsl_m)
: ThreadSafeQueue(cond, unvrsl_m, 1) {}
ThreadSafeQueue(std::condition_variable* cond, std::mutex* unvrsl_m,
uint32_t capacity)
: cond(cond),
m(unvrsl_m),
head(0),
tail(0),
capacity(capacity),
buffer((T*)malloc(get_size() * sizeof(T))),
scratch_space((T*)malloc(get_size() * sizeof(T))) {}
std::condition_variable* cond;
~ThreadSafeQueue() {
free(scratch_space);
free(buffer);
}
void resize(uint32_t new_cap) {
std::unique_lock<std::mutex> lock(*m);
check_params_resize(new_cap);
free(scratch_space);
scratch_space = buffer;
buffer = (T*)malloc(sizeof(T) * new_cap);
copy_cyclical_queue();
free(scratch_space);
scratch_space = (T*)malloc(new_cap * sizeof(T));
tail = get_size();
head = 0;
capacity = new_cap;
}
void enqueue(const T& value) {
std::unique_lock<std::mutex> lock(*m);
resize();
buffer[tail++] = value;
if (tail == get_capacity()) {
tail = 0;
} else if (tail > get_capacity())
throw("Something went horribly wrong TSQ: 75");
cond->notify_one();
}
// Assuming m has already been locked by the caller...
void dequeueAll(std::vector<T>* vOut) {
if (get_size() == 0) return;
scratch_space = buffer;
copy_cyclical_queue();
vOut->insert(vOut->end(), buffer, buffer + get_size());
head = tail = 0;
}
// Const functions because they shouldn't be modifying the internal variables
// of the object
bool is_empty() const { return get_size() == 0; }
uint32_t get_size() const {
if (head == tail)
return 0;
else if (head < tail) {
// 1 2 3
// 0 1 2
// 1
// 0
return tail - head;
} else {
// 3 _ 1 2
// 0 1 2 3
// capacity-head + tail+1 = 4-2+0+1 = 2 + 1
return get_capacity() - head + tail + 1;
}
}
uint32_t get_capacity() const { return capacity; }
//---------------------------------------------------------------------------
private:
std::mutex* m;
uint32_t head;
uint32_t tail;
uint32_t capacity;
T* buffer;
T* scratch_space;
uint32_t get_next_empty_spot();
void copy_cyclical_queue() {
uint32_t size = get_size();
uint32_t cap = get_capacity();
if (size == 0) {
return; // because we have nothing to copy
}
if (head + size <= cap) {
// _ 1 2 3 ... index = 1, size = 3, 1+3 = 4 = capacity... only need 1 copy
memcpy(buffer, scratch_space + head, sizeof(T) * size);
} else {
// 5 1 2 3 4 ... index = 1, size = 5, 1+5 = 6 = capacity... need to copy
// 1-4 then 0-1
// copy number of bytes: front = 1, to (5-1 = 4 elements)
memcpy(buffer, scratch_space + head, sizeof(T) * (cap - head));
// just copy the bytes from the front up to the first element in the old
// array
memcpy(buffer + (cap - head), scratch_space, sizeof(T) * tail);
}
}
void check_params_resize(uint32_t new_cap) {
if (new_cap < get_size()) {
std::cerr << "ThreadSafeQueue: check_params_resize: size(" << get_size()
<< ") > new_cap(" << new_cap
<< ")... data "
"loss will occur if this happens. Prevented."
<< std::endl;
}
}
void resize() {
uint32_t new_cap;
uint32_t size = get_size();
uint32_t cap = get_capacity();
if (size + 1 >= cap - 1) {
std::cout << "RESIZE CALLED --- BAD" << std::endl;
new_cap = 2 * cap;
check_params_resize(new_cap);
free(scratch_space); // free existing (too small) scratch space
scratch_space = buffer; // transfer pointer over
buffer = (T*)malloc(sizeof(T) * new_cap); // allocate a bigger buffer
copy_cyclical_queue();
// move over everything with memcpy from scratch_space to buffer
free(scratch_space); // free what used to be the too-small buffer
scratch_space =
(T*)malloc(sizeof(T) * new_cap); // recreate scratch space
tail = size;
head = 0;
// since we're done with the old array... delete for memory management->
capacity = new_cap;
}
}
};
// Event Types
// keyboard/mouse
// network
// dirty flag
Main.cpp:
#include <unistd.h>
#include <cstdint>
#include <iostream>
#include <mutex>
#include <queue>
#include <sstream>
#include <thread>
#include "ThreadSafeQueue.h"
using namespace std;
void write_to_threadsafe_queue(ThreadSafeQueue<uint32_t> *q,
uint32_t startVal) {
uint32_t count = startVal;
while (true) {
q->enqueue(count);
cout << "Successfully enqueued: " << count << endl;
count += 2;
sleep(count);
}
}
void sleep_and_set_redraw(int *redraw, condition_variable *cond) {
while (true) {
sleep(3);
__sync_fetch_and_or(redraw, 1);
cond->notify_one();
}
}
void process_events(vector<uint32_t> *qOut, condition_variable *cond,
ThreadSafeQueue<uint32_t> *q1,
ThreadSafeQueue<uint32_t> *q2, int *redraw, mutex *m) {
while (true) {
unique_lock<mutex> lck(*m);
cond->wait(lck);
q1->dequeueAll(qOut);
q2->dequeueAll(qOut);
if (__sync_fetch_and_and(redraw, 0)) {
cout << "FLAG SET" << endl;
qOut->push_back(0);
}
for (auto a : *qOut) cout << a << "\t";
cout << endl;
cout << "PROCESSING: " << qOut->size() << endl;
qOut->clear();
}
}
void test_2_queues_and_bool() {
try {
condition_variable cond;
mutex m;
ThreadSafeQueue<uint32_t> q1(&cond, &m, 1024);
ThreadSafeQueue<uint32_t> q2(&cond, &m, 1024);
int redraw = 0;
vector<uint32_t> qOut;
thread t1(write_to_threadsafe_queue, &q1, 2);
thread t2(write_to_threadsafe_queue, &q2, 1);
thread t3(sleep_and_set_redraw, &redraw, &cond);
thread t4(process_events, &qOut, &cond, &q1, &q2, &redraw, &m);
t1.join();
t2.join();
t3.join();
t4.join();
} catch (system_error &e) {
cout << "MAIN TEST CRASHED" << e.what();
}
}
int main() { test_2_queues_and_bool(); }
I need to write a table from a file in a map > m.
I have 3 strings (state, next_states, outputs)
and i want to write them like this (I know it's not correct)
for (i=0; i < 5; i++)
{
//here I have a code, the 3 strings change for every line
for(j=0; j<m.size(); j++)
{
m[i][j] = "state" + "next_states" + "outputs";
}
}
I thought maybe with iterator it would be better but i don't know how to do it.
Here is how you can iterate through your map:
#include <iostream>
#include <map>
#include <vector>
#include <string>
int main()
{
std::map <std::string, std::vector<std::string>> m =
{
{"1", {"a", "b"}},
{"2", {"c", "d", "e"}},
};
for (auto& element : m)
{
std::cout << element.first << " : ";
for (auto& str : element.second)
{
std::cout << str << "; ";
}
std::cout << std::endl;
}
}
In my source file when I define my object s, Visual Studios says Identifier "stack" is undefined. I'm pretty sure I've separated the headers correctly but I don't know why I'm getting this error. Also side note when I put everything together in one source file and compile it, it just exits immediately without any reason. Thanks for the help in advance.
This is the source file
// pa3.cpp : Defines the entry point for the console application.
//
#include "stack.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
//#include <ctype.h>
using namespace std;
int main()
{
//int count;
stack s; //assign s object to stack
string input;
cout << "Please enter the name of the input file: \n";
//cin >> input;
getline(cin, input);
ifstream file(input);
string readline;
//ifstream file(input);
while (getline(file, readline)) //take first line of file and assign to readline
{
s.push(readline); //send it off to push
s.retrieveUPPER();
//file.close();
system("pause");
}
return 0;
}
This is the stack.h file
//#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
//#include <ctype.h>
using namespace std;
class stack
{
public:
int count;
void push(string);
//void pop();
void check(string);
void retrieveUPPER();
private:
static string firstline[1];
static string diskeywords[3];
static char upperword[100];
static char lowerword[100];
static char operatorsarr[100];
static char delimitersarr[100];
};
This is the stack.cpp
#include "stack.h"
#include "stdafx.h"
#include <iostream>
string stack::firstline[1] = { 0 };
string stack::diskeywords[3];
char stack::upperword[100];
char stack::lowerword[100];
char stack::operatorsarr[100];
char stack::delimitersarr[100];
void stack::retrieveUPPER()
{
for (int i = 0; i < 100; i++)
{
cout << upperword[i] << "\n";
}
}
void stack::push(string readline)
{
firstline[0] = readline;
count++;
check(readline);
}
void stack::check(string readline)
{
int length;
char letter;
int leftperenthe = 0;
int rightperenthe = 0;
//int capital;
//int wordFOR;
//int wordBEGIN;
//char keywords[3][8] = { "FOR", "BEGIN", "END" };
char operators[] = "+-=*/;";
char delimiters[] = { ',',';' };
length = readline.length();
for (int i = 0; i < length; i++)
{
for (int j = 0; j < 5; j++)
{
letter = readline[i];
if (isupper(letter)) //if capital letter
{
upperword[i] = letter;
}
else if (islower(letter)) //if lowercase letter
{
lowerword[i] = letter;
}
else if (letter == operators[j]) //if encounters a operator
{
operatorsarr[i] = letter;
}
else if (letter == delimiters[j]) //if encounters a delimiter
{
delimitersarr[i] = letter;
}
else if (letter = ' ') //if encounters a space
{
lowerword[i] = ' ';
operatorsarr[i] = ' ';
delimitersarr[i] = ' ';
}
else if (letter = '(') //if left perenthesis
{
leftperenthe++;
}
else if (letter = ')') //if right perenthesis
{
rightperenthe++;
}
}
}
}
I also had a similar situation:
#include<iostream>
#include<stack.h>
#include<vector>
using namespace std;
int main();
std::vector<int>stickPan (std::vector<int>arr)
{
stack<int>s;
...
}
I used VScode, used mingw-w64, and added paths for * .h files to .json files, but it didn't work.
Errors are reported:
identifier "stack" is undefined.
type name is not allowed.
identifier "s" is undefined
Im not sure what to do i want it to print 0000 to ending in BBBB i was trying to use the printf statement anyways, if anyone can help me figure this out that would be great. Thanks
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
char digits[] = "0123456789AB";
for (int column1=0; column1<=12; column1++) {
for (int column2=0; column2<=12; column2++) {
for (int column3=0; column3<=12; column3++) {
for (int column4=0; column4<=12; column4++) {
std::cout<< digits[column2]<<endl;
}
}}}
return(0);
}
The 4 for loops are not the prettiest thing ever, but they should work and I'm not sure it's worth the complications to do it differently. So keep what you have, just print all digits:
std::cout<< digits[column1]<< digits[column2] << digits[column3] << digits[column4]<<endl;
It's better to parametrize the base and the column count to avoid many nested for's.
#include <iostream>
const int columnCount = 4, base = 12;
char digitToChar(int digit) {
if(digit >= 0 && digit <= 9) {
return '0' + digit;
} else {
return 'A' + digit - 10;
}
}
bool increment(int* number) {
int currentColumn = columnCount - 1;
++number[currentColumn];
while(number[currentColumn] == base) {
number[currentColumn] = 0;
--currentColumn;
if(currentColumn < 0) {
return false;
}
++number[currentColumn];
}
return true;
}
void outputNumber(int* number) {
for(int i = 0; i < columnCount; ++i) {
std::cout << digitToChar(number[i]);
}
std::cout << std::endl;
}
int main() {
int number[columnCount] = {0, 0, 0, 0};
bool overflow = false;
do {
outputNumber(number);
overflow = !increment(number);
} while(!overflow);
return 0;
}