ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
here is my problem and i dont know C++ so i tried comming here to find help. What i need to do is take in a text file the first line as a 2d array breaking the two elements with a colon
the second line as a variable A
and the third line a variable B
and the 4th line a variable c the problem is that i dont know how to parse seperate lines like this is c++
the format of the text file is as follows
A:1 B:2 c:3
4
3
2
my code is as follows
// reading a text file
#include <iostream>
#include <fstream>
#include<iomanip>
//global declaration
using namespace std;
//Function to take in Alpahbet
int AlphabetIn (char alpha[40][40], int varA[01], int varB[01])
{
//Variables
int x, y=0, computer=0;
//Instructions
ifstream filein ("alphabet.txt");
if (filein.is_open())
{
//while file has not reached eof ( end of file built in)
while (! filein.eof() )
{
//get the first line of text THERE IS STILL A PROBLEM RIGHT HERE
filein.getline (alpha[y], 02, ':');
filein >> varA[y];
filein >> varB[y];
y++;
computer++;
}
//close file
filein.close();
}
else
//catch error in reading file since it did not open correctly
cout << "Ooops";
//output what was captured to determine where the error occured in reading in.
for (y=0; y<computer; y++)
{
for(x=0; x<30; x++)
{
cout<<alpha[y][x];
}
cout<<VarA[y] <<" " <<VarB[y];
}
cout<<"\n\n";
return Computer;
}
//MAIN
void main ()
{
//Variables
char alpha [80][30]={0};
int varA [02]={0};
int varB [02]={0};
Please post program code in [code] tags. This preserves whitespace and uses a fixed width font, which really helps people to read your post.
This really sounds like a homework problem. I will not provide you with working code (you would not learn anything by copy-pasting someone else code), but I am happy to guide you.
I do not really understand the description of the problem as you provided it. It would help to see what the input file looks like, and the expected output.
You don't want to loop, reading each element of the "array", and also try to read the last three lines each time. You want to loop, reading the multiple elements, and then after you're done with that, get the last three lines.
Why bother with one-element integer arrays? Just use pointers or references.
You have inner loops to go through your "2d" array, but what is supposed to be in this array? Your input file only has one line for this. I assume the array is supposed to look like this?
A B c
1 2 3
I am a little confused, because you made it 40x40, instead of 2x40.
Don't use leading 0s for literals (like 01, 02) unless you really mean the numbers to be octal.
You also have some capitalization errors in your variable names. Remember, C++ is case-sensitive.
I don't recommend messing with C arrays unless you need to. Since you have character values associated with numbers, apparently (A:1, B:2, c:3), why don't you use a map<string, int>? A map is an associative container, so for instance you could say map["A"] == 1, map["B"] == 2, and you can iterate through it and get the pairs of values.
this is actually for my research so im not trying to learn c++ but were trying to run evaluations on certain algorithms. im not a coder and dont pretend to be as i am a math major. here is what it should do and i hope i can explain this better.
The text file is composed of 4 lines
the first line should be a letter followed by a colon then followed by a number for example A:1
this should be read in and then put into 2 corresponding arrays. the first array will contain the letter and the second array the number equivalent so that later a word such as cab can be converted to a number equivalent. in the case of CAB it would be equivalent to 312.
the second line of the text file is going to be a variable that i will store as A
the third line will be a variable stored as B
and the fourth line a variable stored as C
so a text file would look something like this
A:1 B:2 C:3
4
5
6
and a correct output would be
array 1 = ABC
array 2 = 123
A=4
B=5
C=6
I really appreciate all help anyone can give me i do again want to state that this is not a homework assignment for a class. The code that i posted earlier is what i could figure out based on come online tutorials. again i really appreciate the forum being as helpful as it is and helping out people that are new to this stuff.
First, even though a lot of examples do use the "using namespace std;" to shorten the written code a bit, do not use it in your programs. Even if you didn't use your own namespaces (which on the other hand may ease up your work), just learn it from the beginning -- it's easier later on that way -- that you specify the namespace you use every time you use something from the namespace. So:
Code:
// Not like this
using namespace std;
cout << "Using namespace feels good..";
But instead:
Code:
// This way you don't mess up things accidentally
std::cout << "A better way";
I know there are people who are hard against using std:: in front of every thing used from a namespace and rather tell you to use "using namespace..." if your app only uses the std namespace, but think about it this way: there is a reason why you can do std::cout instead of the other way, and when you learn something, it's a lot harder to forget-and-re-learn it the other way later than if you had thought about it in the first place.
Second thing (in addition to using code tags and saying it explicitly if this is a homework, to get proper answers and no angry posts): start off with simple things. If something doesn't work in your code or you don't get something fully, don't first write a 100+ or 1000+ lines long program and then start looking for errors -- instead isolate the problematic part into a program as small as possible, find a solution and then implement it into the big program you're creating. So if you have problems with file I/O, better write a very small program first that only reads a line (or writes) from (to) a file, and when it works without trouble, modify it so that it reads only certain part of the file, and when that works, so that it reads another piece of the file and so on. When you have the reading part working, put the thing into your big program and start working out next part.
And since C++ is object-oriented, consider if you're better off writing a main file with seven functions or if you're better off writing a class with some more general methods and then using that class. Even if it doesn't make sense in your current small program right now, you might well face a situation where you can re-use your class or it's methods. For example the file reading part; you could write a method that reads a given file in the way you need it to read it and returns the content (or parts of it) or stores them into an array or something, and when you've finished that method and made sure it works, you can simply include it into your program and use the method -- and focus on other things. Using just functions is C-style, and good in certain kinds of programs, but if you're really into C++ you're not getting everything out of it without considering the use of classes and their methods. If you find out you're writing similar code in more than one place, you might be easier off writing a general class, in some cases writing derived class(es) might help further. It depends on the program design really, but because C++ can do it, you should at least think about it.
Underlining the last sentence: think before writing. A lot of people can write, but a whole lot less is able to think enough to make sensible, usable, good code that is shorter and easier to write in the end. Well designed is half done.
EDIT: if it wasn't homework after all, sorry about that. There's just so many who try to feed their homework into the forums here hoping that somebody just gives them an answer without anybody needing to think or learn anything.
If you're a math-head, then C++ is going to cook your noodle because it is a total dogs dinner of a language. Not a lot of elegance in it, but it can be useful. ;-)
Is there a particular reason to use C++ and the STL container classes? I'm asking simply because this sort of processing is a lot more elegantly done with other languages.
It would simply things to split off parts of the problem into functions. One useful function would be to split a string into a vector of sub-strings based on some delimiter character. You could use that to split the whole of the first line into the three chunks, and also for each chunk into the bit before and after the colon.
A prototype of such a function might look like this:
to give more background about the project we are using c++ because a large portion of the project uses the NTL libraries. My job was to evaluate a few algorithms for affine map transformations if anyone is familiar with these. That is why im having such difficulty importing this text file since im not familiar with coding to being with. My job is really to evaluate a set of algorithms. While there is a c++ program already written for this because of the amount of algorithms i have to evaluate it is easier to write them out in text files than manually input everything every time. Thats why im allowed to come on here and use whatever code is given to me because this research project is not graded its for graduate case studies and the code is irrelevant. Part of the project that was given to me is done in c++ with an NTL library and even i dont know what that is. So the professor im working for is out of town and his e-mail he sent to me was just to check online because i should be able to change something to make it easier. He is the one telling me to go online and get something to get the outputs before he is back in town. If i couldnt figure it out before then he said ill just have to manually input everything. I hope this clarifies what im trying to do to anyone or gives a better understanding. Again i thank everyone for taking a look at this post.
typedef map<int, int> iimap;
int AlphabetIn (iimap& alpha, int& varA, int& varB, int& varC) {
...
while (filein.peek() != '\n' ) {
int index;
filein >> index;
filein.ignore(256, ':'); //Ignores up to next colon, within 256 characters.
filein >> alpha[index];
++y;
}
...
The rest is the same, except for changing "simap" to "iimap" (and that's just an arbitrary name anyway, so you can leave them the same, or give it a better name.)
Also, the robustness against weird input (spaces or commas in the integers) is nonexistent. Feel free to extend.
Of course, the previous code also worked for pairs of numbers, it just stored the first number as text.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.