-
String to Int
I searched the FAQ and found something similar to what I needed but when I tried to incorporate it into my code it didn't work out.
Basically I have a little program that opens up two files, one containing questions, the other containing answers to said questions. Then it randomizes the order and selects 25 of these. Now my problem is that this all uses alot fo arrays and every aray must match the number of lines in the two files. Being the lazy person I am I wanted to be able to make a file, put in the number of lines in each file and then have the program read it and set that as the array size. I've tried opening a file, retrieving the string to an array and turning it into an int, but the problem is that the arrays are global variables and must be declared before I can run any functions (well so far as I know).
Code:
#define normal "normal"
#define practice "practice"
#define yes "yes"
#define no "no"
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <Windows.h>
#include <stdio.h>
using namespace std;
string questions[69], answers[69], n_questions[69], n_answers[69], questions_25[25], answers_25[25];
//these are the arrays the questions & answers are stored in
string tchoice2, mi_choice, u_answer, u_status="ERROR", m_choice, time_a;
int j=0, m=0, last, i=0, k=0, q=1, u_wrong=0, w=1, err_check=0, c=0, set_mode=0, ghy=69, ex_chk=0;
int tmd_m=0, h1, h2, m1, m2, s1, s2;
double per_right, u_right=0, versionnum=1.4;
int getanswers(); //retrieve answers from answers.dq
int main_menu(); //main menu when program turns on
int getquestions(); //retrieve questions from questions.dq
int randomizer(); //randomly order questions & answers
int select_25(); //chooses 25 questions & answers from the randomized n_ arrays
int u_help(); //user help menu and articles
int test(); //where the test code lies
int addscores(); //write scores to scores.dq
int norpmode(); //practice or normal mode
int misc_menu(); //miscellaineous menu
int v_info(); //reads version info
int l_questions(); //how many lines are in questions.dq
int l_answers(); // how many answers are in answers.dq
int tmdmode(); //sets timed mode or not
int settime_b(); //starts timer
int settime_e(); //ends timer and figures out how long test took
int u_exit( string e_choice ); //exit feature
int startup();
int main()
{
int startup();
if (ex_chk == 0 )
{
cout << "Welcome, prepping system...\n\n";
string tchoice;
getquestions(); //retrieves questions from questions.dq
last = i;
i = 0;
getanswers(); //retrieves answers from answers.dq
if (err_check != 1)
{
main_menu(); //main menu
while (w==0)
{
randomizer();
select_25();
i = 0;
k = 0;
j = 0;
test();
cout << "\nSave Scores? ";
getline(cin, tchoice2);
if (tchoice2.compare(no) == 0 )
{
}
else if (tchoice2.compare(yes) == 0 )
{
addscores();
}
cout << "\nTake test again? ";
getline(cin, tchoice);
if (tchoice.compare(no) == 0 )
{
w = 1;
}
else if (tchoice.compare(yes) == 0 )
{
norpmode();
}
else
{
cout << "ERROR - 002!\n";
}
}
cout << "\n";
}
system("PAUSE");
return EXIT_SUCCESS;
}
}
-
Re: String to Int
Sounds like you need to do some redesign. If you are writting these questions to disk you should format in someway, such as in XML format. For instance,
Code:
<Trivia>
<id>1</id>
<question_text>What is the capital of Albania?</question_text>
<answer_text>Tirane</answer_text>
<Trivia>
You can either write or download a xml package that does this.
You can then organize your answers and questions nicely into classes.
It requires a little work on your part, but in the end you will have a cleaner design and less headaches.
-
Re: String to Int
I'm not very familiar with XML (though it does look promising), but my problem was that I was retrieving a number from a file into a string (only way I can as far as I know) and was trying to convert it into a int but can't figure out how.
-
Re: String to Int
atoi can convert a string to an int.
Kelly