Note that there are some explanatory texts on larger screens.

plurals
  1. POVector of structs and custom functions acting up
    primarykey
    data
    text
    <p>I am trying to create a C++ project for my Geometry class. I want the user to be able to store and access variables. To do this, I created a <code>struct var</code> containing <code>string name</code> and <code>float value</code>. I have a <code>vector &lt; var &gt; varList</code> in which to hold the variables. However, upon compiling, the program doesn't function well… at all. At first, it checks to see if the variable "dog" exists, which it obviously doesn't, and finds that it does. It then tries to change the variable dog and <code>changeVar</code>, instead of returning <code>ERR_NONEXISTENT</code>, returns a proper exit status of zero. Upon checking the variable, it sees that it doesn't exist. Then, when attempting to list all variables, it creates a segmentation fault. See below:</p> <pre> Building Generator 1.0 Alpha Variable Systems Test ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Enter a variable name: dog Enter a value (A FLOAT!): 2.2 Checking to see if dog exists. It exists! Changing variable. Function returned 0 Enter a variable to check: dog Variable "dog" doesn't exist! Segmentation fault </pre> <p>My source is <a href="http://www.mediafire.com/?8yubuftck5srecg" rel="nofollow">here</a>. I am compiling with Eclipse Helios, with G++ 4.2.1, on Mac 10.6.7 Snow Leopard. What's happening?</p> <p>If this doesn't work, I'll try to figure out <code>std::map</code>…</p> <p>Also, this is only my second question here; please excuse (but notify me of) any formatting mistakes.</p> <p>Thanks,</p> <p>BF</p> <p>EDIT: Here's some code:</p> <p>vsystem.cpp</p> <pre><code>/* * vsystem.cpp * * Created on: Apr 29, 2011 * Author: wjc */ #include &lt;string&gt; #include &lt;vector&gt; #include &lt;sstream&gt; #include "vsystem.h" using namespace std; vector &lt;var&gt; varList; int addVar(string varName, float value){ // Check to see if varName already exists bool varExists = false; for (unsigned int i=0; i&lt;varList.size(); i++){ if (varList[i].name == varName){ varExists = true; return ERR_VAR_EXISTS; } } // Good! The variable doesn't exist yet. var tempVar; tempVar.name = varName; tempVar.value = value; varList.push_back(tempVar); return 0; } int changeVar(string varName, float newValue){ // Check to see if varName exists for(unsigned int i=0; i&lt;varList.size(); i++){ if(varList[i].name != varName){ // If it doesn't match… if (i == varList.size() - 1) // And it's the last one… return ERR_NONEXISTENT; // Uh oh! } else { // Found it! varList[i].value = newValue; } } return 0; } fetchResult fetchVar(string varName){ fetchResult returnValue; // Check to see if varName exists for(unsigned int i=0; i&lt;varList.size(); i++){ if(varList[i].name != varName){ // If it doesn't match… if (i == varList.size() - 1){ // And it's the last one… returnValue.good = false; // Uh oh! returnValue.result = -1; } else { returnValue.good = true; returnValue.result = varList[i].value; } } } return returnValue; } bool checkVar(string varName){ // Check to see if varName exists for(unsigned int i=0; i&lt;varList.size(); i++){ if(varList[i].name != varName){ // If it doesn't match… if (i == varList.size() - 1) // And it's the last one… return false; // It's not here. }else break; } return true; } vector &lt; var &gt; getVarList(){ return varList; } string getVarList(string varDelim, string valueDelim){ stringstream final; for (unsigned int i=0; i&lt;varList.size()-1; i++){ final&lt;&lt;varList[i].name&lt;&lt;valueDelim&lt;&lt;varList[i].value&lt;&lt;varDelim; // add variable name, delim 1 (probably tab), variable value, delim 2 (probably newline) } final&lt;&lt;varList.back().name&lt;&lt;valueDelim&lt;&lt;varList.back().value; // same, but don't add a newline (or other) return final.str(); } </code></pre> <p>vsystem.h</p> <pre><code>/* * vsystem.h * * Created on: Apr 29, 2011 * Author: wjc */ #include &lt;vector&gt; #include &lt;string&gt; #include "consts.h" using namespace std; #ifndef VSYSTEM_H_ #define VSYSTEM_H_ struct fetchResult { float result; bool good; }; struct var { string name; float value; }; int addVar(string varName, float value); int changeVar(string varName, float newValue); fetchResult fetchVar(string varName); bool checkVar (string varName); vector &lt; var &gt; getVarList(); string getVarList(string varDelim, string valueDelim); #endif /* VSYSTEM_H_ */ </code></pre> <p>ui.h</p> <pre><code>/* * ui.cpp * * Created on: Apr 26, 2011 * Author: wjc */ #include &lt;iostream&gt; #include &lt;vector&gt; #include "filedaemon.h" #include "vsystem.h" using namespace std; int runUI(){ cout &lt;&lt; " Variable Systems Test "&lt;&lt;endl; cout &lt;&lt; "~~~~~~~~~~~~~~~~~~~~~~~~~~~"&lt;&lt;endl; cout &lt;&lt; endl; cout&lt;&lt;"Enter a variable name:"&lt;&lt;endl; string varname; cin&gt;&gt;varname; cout&lt;&lt;"Enter a value (A FLOAT!):"&lt;&lt;endl; float value; cin&gt;&gt;value; cout&lt;&lt;"Checking to see if "&lt;&lt;varname&lt;&lt;" exists."&lt;&lt;endl; bool alreadythere = checkVar(varname); alreadythere ? cout&lt;&lt;"It exists!"&lt;&lt;endl : cout&lt;&lt;"It doesn't exist."&lt;&lt;endl; if (alreadythere){ cout&lt;&lt;"Changing variable. Function returned "&lt;&lt;changeVar(varname, value)&lt;&lt;endl; } else { cout&lt;&lt;"Setting variable. Function returned "&lt;&lt;addVar(varname, value)&lt;&lt;endl; } cout&lt;&lt;"Enter a variable to check:"&lt;&lt;endl; string varcheck; cin&gt;&gt;varcheck; fetchResult result = fetchVar(varcheck); if(! result.good){ cout&lt;&lt;"Variable \""&lt;&lt;varcheck&lt;&lt;"\" doesn't exist!"&lt;&lt;endl; } else { cout&lt;&lt;"Variable \""&lt;&lt;varcheck&lt;&lt;"\" is equal to "&lt;&lt;result.result&lt;&lt;endl; } cout&lt;&lt;getVarList("\n","\t")&lt;&lt;endl; string exitstr; cin&gt;&gt;exitstr; return 0; } </code></pre> <p>main.cpp just calls runUI()</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload