Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4568628
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:06:13+00:00 2026-05-21T19:06:13+00:00

I am trying to create a C++ project for my Geometry class. I want

  • 0

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()

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:06:13+00:00Added an answer on May 21, 2026 at 7:06 pm

    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 varList that search for an item to something more simple and easier to read, like:

    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 matches,
                return true;        
            }   
        }   
    
        // If execution reaches here, it means it didn't found a match
        return false;
    }
    

    This solves the crash. I don’t know if your application has any other bugs, but this is my current output:

    Building Generator 1.0 Alpha
       Variable Systems Test   
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    Enter a variable name:
    dog
    Enter a value (A FLOAT!):
    2.2
    Setting variable. Function returned 0
    Enter a variable to check:
    alpha
    Variable "alpha" doesn't exist!
    dog     2.2
    

    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:

    #ifndef CONSTS_H_
    #define CONSTS_H_
    
    #define VERSION "1.0 Alpha"
    
    // Variable errors
    #define ERR_NONEXISTENT 0
    #define ERR_VAR_EXISTS 1
    
    // File r/w errors
    #define ERR_FILE_OPEN 2
    
    #endif /* CONSTS_H_ */
    

    vsystem.cpp:

    #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 ERR_NONEXISTENT;
    }
    
    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 match, replace the value
                            varList[i].value = newValue;
                return ERR_VAR_EXISTS;
                    }
            }
    
        return ERR_NONEXISTENT;  // Uh oh!
    }
    
    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 matches
                            if (i == varList.size() - 1)
                {
                                    returnValue.good = true;
                                    returnValue.result = varList[i].value;
                        return returnValue;
                            }
                    }
            }
    
        returnValue.good = false; // Uh oh!
            returnValue.result = -1;
            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 matches, return true
                        return true;
                    }
            }
    
        // If execution reaches here, it means it didn't found a match
            return false;
    }
    
    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm trying to create a new project and I want to create a
I'm trying to create a build script for my current project, which includes an
I'm trying to create my own error window for the project I am working
Im using Eclipse 3.4, EclipseMe 1.7.9. Im trying to deploy/create package a simple project
When I created the project I'm trying to deploy I selected that I wanted
Trying to create a user account in a test. But getting a Object reference
I'm trying to create a project-like document, in that it contains subdocuments in a
Over the past few days I have been trying to create/run a project in
For a new project I'm trying to create my business classes first and create
I am trying to create a new Java EE project using hibernate and JPA

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.