Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Super Dude
Jan 23, 2005
Do the Jew
How can I add elements to the beginning of a vector? I essentially want to merge a vector and a string, putting one ahead of the other. I was going to just take the characters from the string, starting with the end, and add them to the beginning of the vector. Unfortunately, I can only find the push_back function, which adds it to the end. Would it be ok to make the string into a vector, then tack on the other vector?

edit: Thanks Milde.

Super Dude fucked around with this message at 05:16 on Mar 9, 2008

Adbot
ADBOT LOVES YOU

Super Dude
Jan 23, 2005
Do the Jew
If I have a vector 'alphabet', and I want to erase a single element at index j, is this the correct syntax?

alphabet.erase(j,1);

Super Dude
Jan 23, 2005
Do the Jew
using erase(j) gives this error:

code:
error C2664: 'std::_Vector_iterator<_Ty,_Alloc> std::vector<_Ty>::erase(std::_Vector_iterator<_Ty,_Alloc>)' : cannot convert parameter 1 from 'int' to 'std::_Vector_iterator<_Ty,_Alloc>'1> 

Super Dude
Jan 23, 2005
Do the Jew
(C++)I am trying to grab characters from a string I am reading in from redirecting the cin to a file. Say I have the string "Test" in the file in.txt. I can read the string into my 'string temp' variable. I can write the string to out.txt file as well. My problem is that I want, say, T from the string.

cout << temp[0];

It gives me the following error in gedit when I try to open out.txt

Could not open the file out.txt
gedit has not been able to detect the character encoding.

When I cout to the console, it works fine, but it won't work when writing to a file. What am I doing wrong?

Super Dude
Jan 23, 2005
Do the Jew

Vanadium posted:

Could you post the code and attach in.txt or something?

code:
8
3
# sample input file, three 8x8 floors
#floor 3
XXXXXXXX
X......H
X..X.XXX
X.X..X.X
X..XX..X
X......X
X......X
XXX.vXXX
#floor 2
XXXXXXXX
X......X
X..X.XXX
X.X..X.X
X..XX..X
X......X
X^....SX
XXX.vXXX
#floor 1
XXXXXXXX
X..X.^XX
X....XXX
X..X...X
P..X...X
X...X..X
X...X..X
XXX...XX
code:
cin >> size;
cin >> floors;
int count=0;
int arraySize = (floors*size*size);
char* building = new char[arraySize];

while(getline(cin,temp,'\n'))
{
	if(temp[0]=='#')
	{ 
		comments.push(temp);
	}
	else
	{
		for(int i=0;i<size;i++)
		{
			building[count]=temp[i];
			count++;
		}
	}	
	
}
I suppose I should explain a bit more. We are supposed to start at the point labeled S, and make our way to either the P or the H using a pathfinding algorithm. The floors are arranged in a nxn matrix, with the X being walls, . open spaces...not relevant to my question. Aside from the string class, we can't use the STL. I was planning on inserting all of the floorplans into a 1d array, which would be of size n*n*floors (everything except the top two numbers and the # which start comment lines). I was going to insert the comments into a stack to be reinserted into the out file (this works fine).

The maps are passed to us in a file (such as the one above). We have to write replicas of the maps with our path to another file. I'm using out.txt.

I am running it using: ./proj1 --queue < in.txt > out.txt

It gives the error I mentioned in my previous post when I try to access the buildings array to write to my out file. Also, my count variable (which keeps track of the index in the buildings array) gets up to 199 instead of the 192 max for this example.

Super Dude
Jan 23, 2005
Do the Jew

Vanadium posted:

Can you post the relevant parts of proj1

Edit: Like, for example enough code so that it actually compiles, and ideally produces the output that is not to your expectations.

code:
#include <iostream>
#include <string>
#include "StackList.h"
#include "QueueArray.h"

using namespace std;

int main(int argc, const char* argv[])
{
	int floors, size;
	string temp;
	StackList<string> comments;

	cin >> size;
	cin >> floors;
	int count=0;
	int arraySize = (floors*size*size);
	char* building = new char[arraySize];

	while(getline(cin,temp,'\n'))
	{
		if(temp[0]=='#')
		{ 
			comments.push(temp);
		}
		else
		{
			for(int i=0;i<size;i++)
			{
				building[count]=temp[i];
				if(count==0)
					cout << building[0] << endl;
				count++;
			}
		}	
		
	}

	cout << size << endl << floors << endl;
	string c = " ";
	count = 0;
	while(count<arraySize)
	{
		for(int t=0;t<size;t++)
		{
			for(int u=0;u<size;u++)
			{
				cout << building[count];
				count++;
			}
			cout << endl;
		}		
	}	

	return 0;
}
That tries to do what I want, and should output what I want to out.txt. I was able to open out.txt in vim, it wouldn't open in gedit. It cut off the last line of the map on the very bottom. I realize the comments aren't in there. I don't know what the hell those ^@ things are.

code:
  1 ^@
  2 8
  3 3
  4 ^@^@^@^@^@^@^@^@
  5 XXXXXXXX
  6 X......H
  7 X..X.XXX
  8 X.X..X.X
  9 X..XX..X
 10 X......X
 11 X......X
 12 XXX.vXXX
 13 XXXXXXXX
 14 X......X
 15 X..X.XXX
 16 X.X..X.X
 17 X..XX..X
 18 X......X
 19 X^....SX
 20 XXX.vXXX
 21 XXXXXXXX
 22 X..X.^XX
 23 X....XXX
 24 X..X...X
 25 P..X...X
 26 X...X..X
 27 X...X..X

Super Dude
Jan 23, 2005
Do the Jew

Vanadium posted:

The ^@ are bytes with value 0. gedit does not know what the hell to do with them.

Edit: The cin >> floors just reads the number, and leaves the newline in the stream. So the first getline makes temp an empty string. So reading temp[0], and later temp[i], is undefined behaviour.

I've never used it, but would cin.flush() get rid of the newline?

edit: It seems to be reading that entire first line of the top row of the map as ^@. I've tried cin.flush() and cin.clear(), and neither of those do anything helpful.

edit2: I added another getline right after I read in the two numbers, and before I started reading in the rest of the stuff, and that fixed it. That seems like a really crappy way to fix it though. :(

Super Dude fucked around with this message at 02:27 on Jan 28, 2009

Super Dude
Jan 23, 2005
Do the Jew

Vanadium posted:

You could use std::cin.ignore(std::numeric_limits<int>::max(), '\n'); instead of that getline, or you could just use std::getline in the first place instead of using operator>>, and then use std::strtol or something to read the numbers from the string.

cin.flush() does not do anything that I am aware of, and cin.clear() clears the error bits, not the buffer or anything.

But getline grabs it as a string, right? I needed them to be ints.

Super Dude
Jan 23, 2005
Do the Jew
Alright I have a quick question that is really annoying me. Say I have a 16 bit number, and I need to get bits 10-13, how do I do this? I was thinking that I could do x >> 10 & 0xD but that isn't working.

Super Dude
Jan 23, 2005
Do the Jew
I probably should have explained a little bit better. (I'm using C, so I don't think I can use boost)

Say I have the number

01011001 10100011

and I want to get the value for the range in bold (the value 6).

Super Dude
Jan 23, 2005
Do the Jew

oldkike posted:

You need to shift it right by 10 bits, and then mask off the part you want:

code:
short your_number = 0x59a3;
int six = (your_number >> 10) & 0x7;

Ohhh I get it now. Thanks!

Super Dude
Jan 23, 2005
Do the Jew
If I am using freopen(file_name,"r",stdin); to allow me to use "cin >> temp;" to get info from a file, how do I set it back to the 'regular' "cin" from the keyboard after I am done with the file?

Super Dude
Jan 23, 2005
Do the Jew

Vanadium posted:

You should just create an std::ifstream instead of reopening stdin.

I'm not allowed to use ifstream for some stupid reason. Believe me, I would use it if I could. When I try to reopen stdin, and try to read something in, it just skips over it and doesn't read anything.

Super Dude fucked around with this message at 23:00 on Mar 23, 2009

Super Dude
Jan 23, 2005
Do the Jew

Avenging Dentist posted:

Why can't you use ifstreams, and why is using cin an appropriate alternative?

Because my prof is the devil.

I guess I'm not really sure how to reopen stdin correctly. I'm trying to do this:

I run this: ./program data-file.txt

In the program I:
1) Use freopen(data-file.txt,"r",stdin);
2) Use while(getline(cin,temp)) to retrieve the data
3) ??? reopen stdin to allow input from the user instead of the file ????
4) Use while(getline(cin,other_variable)) to get input from the user at the console

The program is just skipping over the while loop at step 4 and exiting.(that while loop is the last thing in the program).
I don't understand what I need to be doing at step 3 to 'switch input' from the file to the user.

Super Dude
Jan 23, 2005
Do the Jew
I doubt he is expecting us to use weird gimmicky solutions. Is there something other than cin and ifstream that I can use to read from a file?

Here is the quote from the assignment if that helps to clarify what I am saying:

quote:

Your program will read from two input streams and write to a single output stream. The database of songs is read from a file named on the command line. All commands are read from standard input. All output (prompts and results of actions) are written to standard output.

Super Dude
Jan 23, 2005
Do the Jew
I'm writing some regular expression code in flex, and I'm having a problem. I am trying to detect if there are non-printable characters inbetween a set of < >. For some reason, I can't figure out why my current solution is not working.

code:
\<[^>[:print:]]*[^[:print:]][^>]*\>
For instance, my input is: <tag is ^L not valid>

Any ideas?

Super Dude
Jan 23, 2005
Do the Jew

Jabor posted:

So what you're attempting to match is:

1. An open-<
2. Any characters as long as they aren't >
3. A non-printable character
4. Some more any-characters-that-aren't >
5. A close->

The regex you've got (with some liberties taken for whitespace):

code:
\<
[^>[:print:]]*
[^[:print:]]
[^>]*
\>
Hopefully this helps make stuff more apparent.

I guess I'm not seeing what I'm missing because from what I see, my regex matches the description.

Adbot
ADBOT LOVES YOU

Super Dude
Jan 23, 2005
Do the Jew

roomforthetuna posted:

Edit2: Also you could have it match the first non-printable more simply with [^>]*? assuming the specific regexp parser supports that "match shortest" semantic.

Unfortunately ? isn't supported (flex is stupid).

\<([[:print:]]{-}[>\n<])*[^[:print:]][^>]*\>

I think this fixed the problem. It looks horrible and is impossible to read, which really pisses me off.

Super Dude fucked around with this message at 18:52 on Sep 5, 2011

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply