Beginnner seeking help with project

Hello Everyone,
Sorry to bother everyone, but I have a problem I just cannot figure out.I am new to programming and have this assignment that just will not work. I am to create a program using classes that will read different types of test questions such as multiple choice and True and False from the test bank text file. I have created this program but keep getting 4 error reports. first error report is that stoi is not declared, I tried atoi and got the same message. I read about how some compilers do not support C++11, so I deleted code;:blocks and changed to netbeans with C++ builds and still received the same error messages. and now I am back to using Code::Blocks. So now I am thinking maybe it is me that missed something or did something wrong. Driving me crazy.

Please any help would be very much appreciated.


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//the files used
ofstream outfile;
ifstream infile;



class QuestionMC : public Question//class for multiple choice
{
public:
void setQuestion(string theQuestion, int pointValue)
{
string line;
//get the number of choices from the file
getline(infile, line);
numberOfOptions = stoi(line);
question = theQuestion;
value = pointValue;
//get the individual choice lines and load to options array
for (int count = 0; count<numberOfOptions; count++){
getline(infile, line);
options[count] = line;

here is where I keep receiving my first error message:
numberOfOptions = stoi(line);
I keep getting error message stoi is not declared;
same error message when I use atoi
Last edited on
If something is not declared google the something.
Try including ...

#include <stdlib.h> /* atoi */

code::blocks is not a compiler,

"Code::Blocks is a free, open source cross-platform IDE that supports multiple compilers including GCC, Clang and Visual C++. It is developed in C++ using wxWidgets as the GUI toolkit. Using a plugin architecture, its capabilities and features are defined by the provided plugins."
Last edited on
OP's problem is with stoi(), not atoi().

Newbies often don't know the difference between the shiny IDE and the compiler, so you can assume they are using the compiler that ships with Code::Blocks: MinGW.

MinGW requires you to explicitly use C++11, and atoi() is a C++11 function. Here's how to fix it:
http://www.cplusplus.com/doc/tutorial/introduction/codeblocks/

Hope this helps.
Hi CodeWriter and Duoas and thank you very much for your help and time.

I should have mentioned that I did google this error message, and I have seen the responses about C++11 and have followed the instructions to the tee, and after I am still receiving the same error message stoi() not declared, also when I tried to switch to std::atoi I still g0t the same error message.
I have also already tried adding #include <stdlib.h> /* atoi */ and still I get the same error message.
I have read and tried to use stringstream ss
I have uninstalled Code::Blocks and installed DEV C++ from Orwell, from what I was told this version of DEV C++ supports C++11 and still same error message.

I also read somewhere to try adding -std=c++0x
I Also tried downloading Net beans with C++
I tried #include<string> and using std::stoi

yeah I know I am a newbie and I apologize but at this point it is just driving me crazy, I have tried all suggestions to the point where I just want to throw my computer out the window.
I have already submitted my project to my instructor late and not functioning the way I wanted it to. So at this point I just can't let it go.
I need and want to figure out what I am doing wrong.

I promise I did not just run here and ask for help. I feel I gave it my all with the little I know so far.

Anyway Thank you CodeWriter and Duoas, I do very much appreciate the response.
As a BASIC programmer who did some C many years ago C++ is new to me so I am interested to see if I can solve beginner issues as part of that learning process.
If the source code is not too long then post all of it then others can run it to if they have the same problems and often with the whole code it easier to track down any issues.
Last edited on
You probably need a more current version of the compiler and standard library than one shipped with CodeBlocks.

To download MingGW with GCC 5.1.0 and configure it for CodeBlocks: http://www.cplusplus.com/forum/beginner/149018/#msg780212

After that is done, enable C++11 support (see the link that Duoas posted).
Hi JLBorges, and thank you for your help I will try this again , maybe I missed a step the first time, at this point I am willing to try anything.

Hi CodeWriter, Thank you again for your help.

Here are the codes my Instructor and I worked on.

it is kinda long but I will put an <<<< ERROR MESSAGE <<< where my problem is

what I have already tried:

I have seen the responses about C++11 and have followed the instructions to the tee, and after I am still receiving the same error message stoi() not declared, also when I tried to switch to std::atoi I still g0t the same error message.
I have also already tried adding #include <stdlib.h> /* atoi */ and still I get the same error message.
I have read and tried to use stringstream ss
I have uninstalled Code::Blocks and installed DEV C++ from Orwell, from what I was told this version of DEV C++ supports C++11 and still same error message.

I also read somewhere to try adding -std=c++0x
I Also tried downloading Net beans with C++
I tried #include<string> and using std::stoi

I have already completed the class and my final grade was B ... I am doing this cause I need to know what I did wrong or where.
there was this project and another that Killed my final grade.

LOOK FOR <<<<<<<<<< ERROR CODE<<<<<<

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <cstdlib>
#include <exception>
#include <string>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <iomanip>

using namespace std;

//the files used
ofstream outfile; // ERROR MESSAGE  <<<<< ofstream  incomplete does not name a type <<<<<<<<< 
ifstream infile;


class Question // super class
{
public:

	string getQuestion()//gets the question
	{
		return question;
	}
	int getValue() //gets the point value of the question
	{
		return value;
	}

	virtual void setQuestion(string answer, int value)
	{
	}
	virtual void printOptions()
	{
	}

	virtual string getAnswer()
	{
		return answer;
	}

private:
	string question, answer;
	int value;
};


class QuestionTF : public Question// class for true and false questions
{
public:
	void setQuestion(string theQuestion, int pointValue)
	{
		string theAnswer;
		question = theQuestion;
		points = pointValue;
		options = "true/false";
		//get the answer from the file
		getline(infile, theAnswer);
		answer = theAnswer;
	}
	void printOptions()//prints the options for that question
	{
		cout << question << endl;
		cout << answer << endl;
	}
	string getAnswer()//outputs the answer for that question
	{
		return answer;
	}
private:
	string question;
	string answer;
	int points;
	string options;
};



class QuestionMC : public Question//class for multiple choice
{
public:
	void setQuestion(string theQuestion, int pointValue)
	{
		string line;
		//get the number of choices from the file
		getline(infile, line);
		numberOfOptions = stoi(line);   // <<<<<<<<<ERROR MESSAGE <<<<< stoi not declared <<<<
		question = theQuestion;
		value = pointValue;
		//get the individual choice lines and load to options array
		for (int count = 0; count<numberOfOptions; count++){
			getline(infile, line);
			options[count] = line;
		}
		//get the answer from the file and load into answer
		getline(infile, line);
		answer = line;
	}
	void printOptions()// prints the questions, options, and answer
	{
		char first = 'A';
		cout << question << endl;
		for (int count = 0; count<numberOfOptions; count++){
			cout << first++ << ".  " << options[count] << endl;
		}
		cout << "Answer:  " << answer << endl;
	}
	string getAnswer()// prints the answer
	{
		return answer;
	}
private:
	int value, numberOfOptions;
	string question, answer;
	string options[6];
};




int main() {
	Question *myQuestions[10];
	string questiontype, questiontxt;
	string answertxt, optiontxt;
	int numquestions, questionvalue;

	// this is the test bank test file 
	
	outfile.open("testbank.txt");
	if (outfile.is_open())
	{
		outfile << "3\n";
		outfile << "TF 5\n";
		outfile << "There exists birds that cannot fly?\n";
		outfile << "true\n";
		outfile << "MC 10\n";
		outfile << "Who was the President of the USA in 1991?\n";
		outfile << "6\n";
		outfile << "Richard Nixon\n";
		outfile << "Gerald Ford\n";
		outfile << "Jimmy Carter\n";
		outfile << "Ronald Reagan\n";
		outfile << "George Bush Sr.\n";
		outfile << "Bill Clinton\n";
		outfile << "E\n";
		outfile << "TF 10\n";
		outfile << "The city of Boston hosted the 2004 Summer Olympics?\n";
		outfile << "false\n";
		outfile.close();
	}
	else cout << "Unable to open file";

	//opening the testbank file and processing as a question of each type

	infile.open("testbank.txt");
	string line, theQuestion, theAnswer;
	if (infile.is_open())
	{
		//get the number of questions from the first line in the file
	getline(infile, line);
	numquestions = stoi(line); // <<<<<<<<<<  Error MESSAGE <<< stoi not declared <<<<<<
	for (int count = 0; count<numquestions; count++){
	getline(infile, line);
	//get the next line with the question type and the value of the question
	int npos = line.size();
	int prev_pos = 0;
	int pos = 0;
	while (line[pos] != ' ')
	pos++;
        questiontype = line.substr(prev_pos, pos - prev_pos);
	prev_pos = ++pos;
	questionvalue = stoi(line.substr(prev_pos, npos - prev_pos)); // Last word <<<< ERROR MESSAGE stoi not declared

	//process a true/false question
	if (questiontype == "TF")
   	{
	myQuestions[count] = new QuestionTF;
	getline(infile, theQuestion);
	myQuestions[count]->setQuestion(theQuestion, questionvalue);
	}
	//process a multiple choice question
	if (questiontype == "MC")
		{
	myQuestions[count] = new QuestionMC;
	getline(infile, theQuestion);
	myQuestions[count]->setQuestion(theQuestion, questionvalue);
	}

	}
}
 //print out the questions that have been processed
 for (int count = 0; count<numquestions; count++)
{
 myQuestions[count]->printOptions();
 cout << endl;
}

getchar();
return 0;
}




Last edited on
Yeah it definitely has to do with the way you are compiling the program. i think the stoi might not be working because you are aren't like doing std:: or whatever infront of the stoi function. I could be wrong though. Just a guess
> at this point I am willing to try anything.

Temporary fix 1 (if std::strtol() http://en.cppreference.com/w/cpp/string/byte/strtol is available):
http://www.cplusplus.com/forum/beginner/120836/#msg657958

Temporay fix 2 (string stream): http://www.cplusplus.com/forum/general/152105/#msg791139

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
JLBorges OMG thank you very much for the information and for your time and help.

Shamieh Also thank you.

Duoas Thank you

CodeWriter Thank you

I appreciate everyone's help

So this must be the compiler I am using. I just ran this program on C++ shell and from what I see everything ran fine..

However I did receive these two error messages

14:10: error: aggregate 'std::ofstream outfile' has incomplete type and cannot be defined
15:10: error: aggregate 'std::ifstream infile' has incomplete type and cannot be defined

But I am almost sure I can fix that.
It just means you forgot to #include <fstream>

:O)
Topic archived. No new replies allowed.