How to push into values into a stack from a text file - c++

I am trying to teach myself C++ with some programming assignments.
Trying to learn using stacks but I am unsure how to push values from a txt file into a stack.
Let' say I have the following text file:
16 24 25 3 20 18 7 17 4 15 13 22 2 12 10 5 8 1 11 21 19 6 23 9 14
How would I use ifstream,and argv from the command line to push the values into a stack?
Did research and using this as help, but it may not be relevant:
How to push data of different data types into a vector by reading from a file?

Here is something to get you started.
stack<int> data;
{
ifstream file("file.txt");
int i;
while (file >> i)
{
data.push_back(i);
}
}

Related

struct method partly changes the values of it's member array cpp

Some information about the overall project:
I have to find if specific nodes remain connected if i start removing the lowest width edges from a graph. I have a struct solve, which has a member array called connected. In a method of this struct , FindConnections I go over some of the edges, from the Kth till the last and see which nodes are connected. The way I keep track of the connected nodes is to have an array that for each node points to the lowest id node it is connected, with the lowest pointing to itself
for example
if nodes 2 5 6 12 are directly connected
connected[2] =connected[5] =connected[6] =connected[12] = 2
so now if 12 and 23 are connected (and 12 is the lowest connection of 23)
connected [23] = 12 and connected[connected[23]] = 2 so i can reach 2 from 23 with recursion
My problem is that after finishing modifying the connected array inside FindConnections, some of the changes are preserved while other not
Here is the code:
void FindConnections(int index)
{
for (int temp, i = index; i < NumberOfPortals; i++)
{
temp = min(first[i], second[i]); // the nodes which edge i connects
connected[first[i]] = temp;
connected[second[i]] = temp;
}
}
which is called by
void seeAllConnections() // this function is for visualization it will not be included
{
for (int i = NumberOfPortals - 1; i >= 0; --i)
{
printf("Only using %d Portals:\n", NumberOfPortals - i);
FindConnections(i);
seeconnected(); // prints connected array
for (int i = 0; i < NumberOfUniverses; i++) //resets connected array
{
connected[i] = i;
}
}
}
In the two first iterations of the for loop in seeAllConnections, everything is good, the edges that are examined are
first second width(irrelevant for now)
6 7 255
26 2 111
11 7 36
in the beginning everyone is connected with himself
in the first one we get the output
(I am placing ** around the values that are changed and !! around the one that was supposed to change but didn't , just so you can see it better, the program prints just the numbers)
Only using 1 Portals:
connected are:
0 1 2 3 4 5 6 7 8 9 10 *7* 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
and we can see that connected[11] = 7 just like we wanted to
in the second one
Only using 2 Portals:
connected are:
0 1 2 3 4 5 6 7 8 9 10 *7* 12 13 14 15 16 17 18 19 20 21 22 23 24 25 *2* 27 28 29
connected[11] =7 just like before
connected[26] = 2 just like we wanted
in the third one
Only using 3 Portals:
connected are:
0 1 2 3 4 5 6 !7! 8 9 10 *7* 12 13 14 15 16 17 18 19 20 21 22 23 24 25 *2* 27 28 29
connected [7] = 7 , not 6
moreover, when i use gdb, inside the FindConnections loop, connected[7] = 6 like we wanted
(gdb) print first[i]
$10 = 6
(gdb) print second[i]
$11 = 7
(gdb) print connected[first[i]]
$12 = 6
(gdb) print connected[second[i]]
$13 = 6
but when it exits the function and returns to seeAllConnected
connected[7] = 7
What Am I doing wrong? how can the first two changes be preserved form the same function in the same struct in the same loop, while the second one isn't?
Also after every time I call FindConnections I reset the array to it's original values, so the changes couldn't have been preserved from before
Thank you in advance
I found out what was wrong, as it was a reverse iteration connected[7] got overwritten.

C++ : For loop not iterating designated number of times (1 iteration less) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a for loop and a variable C.
The loop begins at 0 and is expected to run C times but actually it runs C-1 times.
Here is my code :
vector<int> v(1000) //Allocated globally
int M, S, C;
cin>>M>>S>>C;
//cout<<M<<" "<<S<<" "<<C;
int fi=0, la=0;
for(int i=0; i<C; i++)
{
int f;
cin>>f;
if(i==0l){ fi = f;}
v[f] = f;
cout<<i<<" "<<f<<" "<<v[f]<<endl;
if(i==C-1){ la = f;}
}
This is my test case -
3 27 16
2
3
5
6
8
9
10
13
14
15
16
19
20
21
22
27
Output by Xcode :
0 2 2
1 3 3
2 5 5
3 6 6
4 8 8
5 9 9
6 10 10
7 13 13
8 14 14
9 15 15
10 16 16
11 19 19
12 20 20
13 21 21
14 22 22
I use Xcode on Mac if it makes a difference.
The variables fi and la are to find the first and the last element of the list.
I want to know what is wrong in my code for the for loop and why is it not iterating C times.
Thanks
Your loop is iterating C times. This is the classic Zero-Based Numbering issue.
Let me explain using your example where C is 16 and a numbered list:
2
3
5
6
8
9
10
13
14
15
16
19
20
21
22
27
So you see your lopp did iterate 16 times. To go from 0 to 16 would have actually been iterating one more time, so 17 times.

Weird __COUNTER__ behavior on LLVM

I'm trying to understand what could cause this problem because everything seems to be fine but the result seems to point out some sort of buggy behavior.
I have a custom struct defined as:
struct MyStruct {
const u16 index;
...
MyStruct(u16 index) : index(index), ... { }
}
static const MyStruct array[] = {
MyStruct(__COUNTER__,...),
MyStruct(__COUNTER__,...),
MyStruct(__COUNTER__,...)
...
}
Now, if I check the preprocessed file with XCode everything is fine and I get incremental indices as they are supposed to be.
At runtime, instead, what happens is that the index 16 becomes 23 and the successive indices are lowered by one, eg:
real index stored index
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 23
17 16
18 17
19 18
20 19
This is quite curious, especially because 16 seems to be a specific bound. Compiler is Apple Clang 4.2 (based on LLVM 3.2), on XCode 4.6.

I seem to be having a issue reading in from a file [duplicate]

This question already has answers here:
Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?
(5 answers)
Closed 8 years ago.
thanks for looking at this in advance.
I can get my code to compile correctly and whatnot, but when running I get stuck in an infinite seeming loop and I have to exit out manually every time. I am new to reading in data from files and I think that is probably my error but any help looking at my code would be appreciated.
In the interest of not submitting some giant file, I'm only going to submit my main function because I assume the error is in there, I'm just not sure of where.
4 #include "class.h"
5 #include <fstream>
6 using namespace std;
7
8
9 int main()
10 {
11 ifstream external; //declaring input stream for my external file
12 //external.open("external.txt"); //telling the compiler what the source of my external file is called
13 //external.close();
14
15 char time[4], course[4], section[3]; //places to store read in values
16 char dept_name[20];
17
18 table hash_table; //instance of my class
19
20 while(!external.eof()) //while it is not the end of the file
21 {
22 external.open("external.txt"); //opens file from location
23
24 external.getline(dept_name, 20); //grabs the info to be input
25 external.getline(time, 4);
26 external.getline(course, 4);
27 external.getline(section, 3);
28
29 external.close(); //closes file until new one must begin
30
31 cin.ignore(5, '\n'); //ignores five characters until next course
32 hash_table.insert(dept_name, course, section, time); //inserts to table
33 }
34 hash_table.display_all();
35 }
Don't open the file for every entry you read. Among other things, opening the file for each entry means you always start reading at the start of the file.
Change this:
20 while(!external.eof()) //while it is not the end of the file
21 {
22 external.open("external.txt"); //opens file from location
23
24 external.getline(dept_name, 20); //grabs the info to be input
25 external.getline(time, 4);
26 external.getline(course, 4);
27 external.getline(section, 3);
28
29 external.close(); //closes file until new one must begin
30
31 cin.ignore(5, '\n'); //ignores five characters until next course
32 hash_table.insert(dept_name, course, section, time); //inserts to table
33 }
to this:
20 external.open("external.txt"); //opens file from location
21 while(!external.eof()) //while it is not the end of the file
22 {
23
24 external.getline(dept_name, 20); //grabs the info to be input
25 external.getline(time, 4);
26 external.getline(course, 4);
27 external.getline(section, 3);
28
29
30 cin.ignore(5, '\n'); //ignores five characters until next course
31 hash_table.insert(dept_name, course, section, time); //inserts to table
32 }
33 external.close(); //closes file

Mystifying For-Loop Error

I am using Ubuntu 10.10, Codeblocks IDE, and gcc compiler. I noticed the program I am writing was creating some odd output. Eventually I narrowed the issue down to a for-loop in the program. I was surprised to discover that the following basic for-loop didn't perform as expected.
#include <iostream>
using namespace std;
int main()
{
for(unsigned int i = 0; i < 21; i++)
{
cout << i << endl;
}
return 0;
}
When I compile and run it, the output is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Although one would expect the output should include zero. Very surprisingly, when I change the for loop to
#include <iostream>
using namespace std;
int main()
{
for(unsigned int i = 0; i < 20; i++)
{
cout << i << endl;
}
return 0;
}
I get the expected output of:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
I can't for the life of me figure out why 21 (and all numbers greater than 21) give me this false output, while 20 (and lower numbers) don't. If anyone has run into anything like this before, I'd sure appreciate hearing how he/she worked around it.
maybe the screen just scroll?
try to redirect the output to a text file
This seemed so weird that i run your first program and got what i would expect :
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
However, i notice that you use gcc as your compiler. This one is aimed towards c programming. Better use g++ as i did for this. It works fine here. (i'm actually surprised gcc compiles that :/)