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 struct var containing string name and float value. I have a vector < var > varList 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 changeVar, instead of returning ERR_NONEXISTENT, 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:
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
My source is here. I am compiling with Eclipse Helios, with G++ 4.2.1, on Mac 10.6.7 Snow Leopard. What’s happening?
If this doesn’t work, I’ll try to figure out std::map…
Also, this is only my second question here; please excuse (but notify me of) any formatting mistakes.
Thanks,
BF
EDIT: Here’s some code:
vsystem.cpp
/*
* vsystem.cpp
*
* Created on: Apr 29, 2011
* Author: wjc
*/
#include <string>
#include <vector>
#include <sstream>
#include "vsystem.h"
using namespace std;
vector <var> varList;
int addVar(string varName, float value){
// Check to see if varName already exists
bool varExists = false;
for (unsigned int i=0; i<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<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<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<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 < var > getVarList(){
return varList;
}
string getVarList(string varDelim, string valueDelim){
stringstream final;
for (unsigned int i=0; i<varList.size()-1; i++){
final<<varList[i].name<<valueDelim<<varList[i].value<<varDelim;
// add variable name, delim 1 (probably tab), variable value, delim 2 (probably newline)
}
final<<varList.back().name<<valueDelim<<varList.back().value;
// same, but don't add a newline (or other)
return final.str();
}
vsystem.h
/*
* vsystem.h
*
* Created on: Apr 29, 2011
* Author: wjc
*/
#include <vector>
#include <string>
#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 < var > getVarList();
string getVarList(string varDelim, string valueDelim);
#endif /* VSYSTEM_H_ */
ui.h
/*
* ui.cpp
*
* Created on: Apr 26, 2011
* Author: wjc
*/
#include <iostream>
#include <vector>
#include "filedaemon.h"
#include "vsystem.h"
using namespace std;
int runUI(){
cout << " Variable Systems Test "<<endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout << endl;
cout<<"Enter a variable name:"<<endl;
string varname;
cin>>varname;
cout<<"Enter a value (A FLOAT!):"<<endl;
float value;
cin>>value;
cout<<"Checking to see if "<<varname<<" exists."<<endl;
bool alreadythere = checkVar(varname);
alreadythere ? cout<<"It exists!"<<endl : cout<<"It doesn't exist."<<endl;
if (alreadythere){
cout<<"Changing variable. Function returned "<<changeVar(varname, value)<<endl;
} else {
cout<<"Setting variable. Function returned "<<addVar(varname, value)<<endl;
}
cout<<"Enter a variable to check:"<<endl;
string varcheck;
cin>>varcheck;
fetchResult result = fetchVar(varcheck);
if(! result.good){
cout<<"Variable \""<<varcheck<<"\" doesn't exist!"<<endl;
} else {
cout<<"Variable \""<<varcheck<<"\" is equal to "<<result.result<<endl;
}
cout<<getVarList("\n","\t")<<endl;
string exitstr;
cin>>exitstr;
return 0;
}
main.cpp just calls runUI()
The way you are looping on the vector and returning true/false is weird and your app is crashing because of
checkVar().I encourage you to change all the loops on
varListthat search for an item to something more simple and easier to read, like:This solves the crash. I don’t know if your application has any other bugs, but this is my current output:
EDIT:
Another problem is with your defines: ERR_NONEXISTENT and ERR_VAR_EXISTS are both
1. They should have different values! I’m pasting the relevant code below:consts.h:
vsystem.cpp: