mahurshi@gmail.com wrote:[color=blue]
> my knowledge in c++ is very limited (i just know the general
> programming concepts and can do a bit of programming in a bunch of
> languages... in other words, jack of all trades but a master of none..
> i just write code for fun occasionally and learned stuff mostly by
> myself, but thats it. so my programming style is taboo in the real
> word :-) ).
>
> i am doing this for a class (we need to make a very simple logic
> simulator), the way we code it/language we use doesn't matter, just the
> functionality matters. so i figured i should go with the simplest one
> first. and if i get to the end and have time, maybe swith over to more
> fancy stuff (just for the heck of learning, grade doesn't change
> anyway)
>
> i am having a very peculiar problem (i am posting my entire code here
> to give a complete understanding of what's going on)
>
> #include <iostream>
> #include <string>
> #include <fstream>
> #include <vector>
>
> using namespace std;
>
> /* Defining sGATE structure */
> struct sGATE
> {
> string name;
> vector<int> input;
> int output;
> };
>
> /* Keeping GATE/NET vectors global for now.. NEED TO CHANGE*/
> vector <sGATE> GATE;
> vector <int> NET;
> sGATE CURRENTGATE;
>
> int main(int argc, char *argv[])
> {[/color]
[snip]
[color=blue]
>
> it should basically read the input file and tell me how many inputs
> each gate has. (if i can get that right i can jump to other stuff
> right :-) )
>
> 1. i checked it while it is reading the inputs (with those cout
> statements, and it seems to work fine because i see pretty much
> everything in the input file, which is good.)
>
> 2. while printing it out, i want it to figure out the length of the
> input <vector> (in most cases, i know it is 2 and for BUF and INV it is
> 1.. but i want it to figure that out by itself) .. this isn't happening
> :-(
> i am thinking i screwed up somewhere in last the printing loop but i
> couldn't get around this problem.
>
> 3. outfile is also not being written to properly, but i think i can
> figure this out, i was spending my time investigating point 2 above.
>
> any help is appreciated.
>
> thanks.
>[/color]
Well one problem is the global variable CURRENTGATE. You've written
'NEED TO CHANGE', well change it now and your program will work.
The problem is that you are using the same CURRENTGATE each time. So
every time that you do CURRENTGATE.input.push_back(...)
CURRENTGATE.input keeps getting longer and longer and longer. You are
assuming that each time you start to read a new gate CURRENTGATE.input
is zero length but that isn't true.
Rewrite your code like this
if (currentword == "INV" || currentword == "BUF")
{
sGATE CURRENTGATE;
...
}
else if (currentword != "INPUT" && currentword != "OUTPUT")
{
sGATE CURRENTGATE;
...
}
else
{
...
}
that way you start with a new CURRENTGATE variable each time you read a
new gate.
And the moral is...?
Global variables are a bad thing, especially in the hands of a newbie.
Hope it's a lesson well learned.
john