I've written a program in VB which exports data into a text file, which I want to be read by another program in C++ and then that data be assigned as a variable. The program is essentially a quiz and the program I wrote in VB is question maker that exports all the data required into the text file.
Below is the text file:
test question|test A|test B|test C|test D|A|100|0|0|0|Right, I know this. The answer is 100% A. Good luck!|B|100|0|
From left to right we have, the question, AnswerA, AnswerB, AnswerC, AnswerD, the correct answer, percent 1, 2, 3, 4, (for polling an audience of what they think the answer may be) a friend's answer, a wrong answer (the program eliminates two wrong answers at one point, this answer is the remaining wrong answer) and percent 1 and 2 again (in case they eliminate 2 wrong answers but still want a poll).
I did quite a lot of google searching but found nothing to my following question (perhaps due to the fact I used the wrong keywords, I'm not too educated when it comes to programming jargon). What I want the C++ program to do is read the file and when it sees "|" it knows that what is coming is a new variable. Would I be better using ifstream or something else and how would I tell the program to identify the "|" and make it read whatever is in between as a variable?
Look at the documentation for istream::getline. You can use the | character as a delimiter.
Related
I realise this question probably has been asked before. I'm asking this as I don't know what to search for.
Hi, so I've started learning about arrays, but I'm not sure how (or if) they can help me with this problem:
Let's say I want the user to be able to choose what to do within my program. I then write a numbered list to the console:
Output "a"
Output "b"
Output "c"
The user will be asked to enter a number, and the number defines which function the program is going to run. What i want the program to do is instead of having me write this three times and changing out the numbers and letters:
if (numberInput == "1") {
functionA;
}
I just write something that takes the number from numberInput and then runs the function associated with it.
Again, I know this question probably has been asked a 1000 times before, so I'm wondering if someone has an idea on how I should go forward on searching for the answer. Thanks!
What you want is called a switch statement
I would start there
i think best here is switch/case stuff
exactly what you're askin about is to use array of function pointers.See
How can I use an array of function pointers?
you'll end up with
int c;
std::cin >> c;
....
(*fnpointers[c])();
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've been working on this C++ project for roughly 2 weeks now and I'm stumped on a couple things regarding 2D Arrays. To start off here is the code I wrote as of now:
http://pastebin.com/vCsz947Q
I decided to provide this as a pastebin link due to the fact that it's rather large (it uses 8 functions) and I thought it would save space in this post. I'm new to this site so I apologize if there's a better way that I'm not aware of.
The Problem:
When I go to save my "char seating" array to a .dat file using the "save seats" function that I created, I get a file that gives me the following garbage character " Ì " instead of the intended ' # ' (for open seats) or ' * ' (if a seat is bought).
My functions will save the intended amount of rows (15) and columns (30) despite this though. Also an asterisk will be placed when I go to "purchase a seat" in this program in the file. Additionally my program loads the files as intended, except for the fact that... Well... There's garbage data stored in the seat array.
I feel like this relates to another problem I'm having where if I go to the "purchase seats" function and I say to purchase a seat, it should replace a # with a *, but it doesn't, yet in the saved file it will show an asterisk in the intended spot... Which is very strange.
I have absolutely no idea why this occurs, and what's frustrating is this one thing that's preventing me from finishing this program. I want to believe that my original array in int main that's being called by other functions isn't being updated properly, but I don't know, which is why I came here to seek assistance.
Thank you for your assistance whoever can help.
Well for a start you have some undefined behaviour here inside your displaySeatingChart (char displaySeats[ ][30], float displayPrices[ ]) function with the following:
const int rowDisplay = 15;
const int colDisplay = 30;
as later within one of your loops you have
cout << displaySeats[rowDisplay][colDisplay];
which is clearly reading beyond the array bounds since in main() you define
const int rowMain = 15;
const int colMain = 30;
char seating[rowMain][colMain];
float seatPrices[15];
and pass both seating and seatPrices to the displaySeats function. There may well be other problems with your code but this at least is a clear example of undefined behaviour. Consider stepping through the code with a debugger to get a clearer idea of the source of the issue.
On another note given that you are working with C++ consider working with std::vector instead of arrays. This will give you more scope to ascertain the dimensions of the items that arrays that you are working with from within your utility functions and result in less potential for errors in array access.
This might seem a bit far fetched and possible off-topic (sorry if it is), but I'd like to know for sure if it is possible or not.
I am working on a Q and A program.
The text file is laid out in a Question tab Answer newline style.
My question is this: Is it possible to read an answer as a function.
Example:
Question - What time is it? / Answer - getCurrentTime()
Question - What is today's date? / Answer - getCurrentDate()
Then the program, though string parsing, knows that this is a function without an argument and calls the function getCurrentTime() or getCurrentDate() which prints the time or date respectively.
This is possible using an array of function pointers. You just load all the functions into the array. How you obtain the correct index is up to you. The only useful way I can come up with is maintain a second array containing the function names in the same positions as the functions in the function array. Then search the function name array and use the index in that array to access the correct function in the function array. If you need a better explanation leave a message. It is very late at night here and I need to work in the morning.
Barmar's solution will work to and is the better way to go about it but use function pointers.
Hope this helps
dannyhut
I am aware that there already exist related threads like how do I loop through file names in stata
I follow those instructions but however receive the invalid syntax r(198) error in Stata.
My code looks as follows:
foreach var of "*/ABC.dta" ABC{
infix observation 1-2 date 3 using "*/CC_ABC`var'.txt"
save "*/CC_ABC`var'.dta" ,replace
}
Where ABC.dta is a list of pretty random numbers which all occur in file names. Do you have an idea why I get errors here?
Thanks a lot!
Is this real code?
Stata wouldn't get past the lack of a listtype following of. This is the very first thing explained in the help for foreach.
If this isn't real code, please show us a real example of something that doesn't work, ideally one that we can reproduce.
(If you don't understand something, imposing your own abstraction or generalization is just likely to muddy your question for those who do.)
I'm using VS2008 to write a program. There's one specific line in my code that causes a numerical error. It is:
Qp[j] = (Cp - Cm)/(Bp + Bm);
Qp is a std::vector. When I comment this line out, the numerical error disappears. I am going through my code line by line to find all the places that access Qp[j]. I was wondering if there was a feature in VS2008 or a linux program that wraps around the executable that can identify every line of code that reads from that section of memory (the specific element in the vector)?
I tried searching online but the keywords I used brought up results relating to global variables.
--- EDIT
Hi all. To those have responded, thank you. Just to clarify my question:
Imagine I have a vector with 5 elements. I'd like to know all the places in my code that use the value stored in element 3 at any point in time during execution. Is there an easy way to do this?
I am not sure if I understand you correctly, but if you comment out that line and the code works then maybe the problem is that line, and you don't need to check others lines.
Maybe in your case you get in the situation where Bp+Bm = 0 (division by zero error).
Qp may not have as many elements as the index j, check the size of Qp.