Thread: I'm lost

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Unhappy I'm lost

    I'm having a lot of trouble with one of my assignments. We are supposed to make a program that inputs text from a file, then separates the incoming data into tokens. And then place each one into a 2-dimensional array of C-strings. Then we are supposed to make a second copy of the array of tokens and use the bubble down function to sort it. Next we are supposed to output the two arrays in two side by side columns of tokens. So far this is what I have.
    Code:
    #include<cctype>
    #include<string>
    #include<iomanip>
    #include<iostream>
    #include<cmath>
    #include<stdio.h>
    #include<fstream>
    using namespace std;
    void gettoken(ifstream& ReadFile, char thetoken, int& k);
    void copyarray(char& temp, char thetoken, char tokencopy);
    void outputarray(char tokencopy, char thetoken);
    void welcomemsg();
    void sortarray(char tokencopy, int& k);
    void inputfilename(ifstream& ReadFile);
    void farewell();
    int main()
    {
    	int k=0;
    	char temp[1000];
    	char tokencopy[500][500];
    	char thetoken[500][500];
    	ifstream ReadFile;
    	welcomemsg();
    	inputfilename(ReadFile);
    	gettoken(ReadFile, thetoken[500][500], k);
    	copyarray(temp[1000], tokencopy[500][500], thetoken[500][500]);
    	sortarray(tokencopy[500][500], k);
    	outputarray(tokencopy[500][500], thetoken[500][500]);
    	farewell();
    	return 0;
    }
    void welcomemsg()
    {
    	cout << "Welcome to Michael's Parser." << endl;
    }
    void inputfilename(ifstream& ReadFile)
    {
    	char pathname[50];
    	cout << "What is the name of the input file? " << endl;
    	cin >> pathname;
    	ReadFile.open(pathname);
    }
    void gettoken(ifstream& ReadFile, char thetoken, int& k)
    {
    	char x;
    	ReadFile.get(x);
    	while(isalpha(x))
    	{
    		thetoken[k] = x;
    		k++;
    		ReadFile.get(x);
    	}
    	thetoken[k+1]='\0';
    }
    void copyarray(char& temp, char thetoken, char tokencopy)
    {
    	int x=0;
    	int y=0;
    	while(isalpha(thetoken[y][0]))
    	{
    		temp = thetoken[y][x];
    		while(isalpha(temp));
    		{
    			temp = tokencopy[y][x];
    			x++;
    			temp = thetoken[y][x];
    		}
    	y++;
    	}	
    }
    void sortarray(char tokencopy, int& k)
    {
    	for(y=0; y < k; y++)
    	{
    		while(isalpha(tokencopy[0]))
    		{
    			while(tokencopy[y]<tokencopy[y+1])
    			{
    				swap(tokencopy[y], tokencopy[y+1]);
    			}
    		}
    	}
    }
    void outputarray(char tokencopy, char thetoken)
    {
    	int y=0;
    	cout << setw(12) << right << fixed << "Original tokens";
    	cout << setw(12) << right << fixed << "Sorted tokens" << endl;
    	while(isalpha(thetoken[y]))
    	{
    		cout << thetoken[y] << "		" << tokencopy[y];
    	}
    }
    void farewell()
    {
    	cout << endl << "Thank you for using Michael's parser. Bye." << endl;
    }
    I know there are tons of errors in there but the one thing I am really having a problem with is when it compiles I get the error "subscript requires array or pointer type" multiple times. I understand it has something to do with reading in the information from the text file and copying it into the array but I just can't figure it out. Also I know the bubble down function is way off so any help with that also would be greatly appreciated, thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're passing arrays to functions, you need the function to expect an array:
    Code:
    void copyarray(char& temp, char thetoken[500][500], char tokencopy[500][500])

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    An example of your problem is in this function;
    Code:
    void gettoken(ifstream& ReadFile, char thetoken, int& k)
    {
    	char x;
    	ReadFile.get(x);
    	while(isalpha(x))
    	{
    		thetoken[k] = x;
    		k++;
    		ReadFile.get(x);
    	}
    	thetoken[k+1]='\0';
    }
    "thetoken" is declared as a single character. But you are accessing it as if it is an array. Subscripts only work for arrays. They do not work for single values.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Usually if your passing a 2 dimensional array, the compiler only needs to know one of the dimensions.

    Code:
    void passArray ( char[][ 20 ] )
    But this is purly programmer prefferance.
    Double Helix STL

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    I am lost too on the same exact program!!

    did you end up figuring this out? I have the same exact project and I am totally lost!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A big fat problem in the code is:
    tokencopy[500][500]
    This accesses the 500th subscript of the 500th element (which btw, doesn't exist).
    To pass the whole array, one simply types the name.
    And it's preferable to use std::string instead of char.

    Plus a 500 x 500 string array is a little too much for the stack. It fills up about a 4th of it (if it's one MB), so it's better to allocate it on the heap.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Unhappy

    this is mine so far...which is nothing...

    #include <iostream>
    #include <fstream>

    using namespace std;

    int main()
    {
    ifstream myfile;

    myfile.open("/Users/Ashley/Desktop/words.txt");

    char words[20][10];
    char wordscopy[20][10];

    int row;
    int col;

    for (row = 0; row < 20; row++)
    myfile.get(words[row],10);

    for(row = 0; row < 20; row++)
    for(col = 0; col < 10; col++)
    {
    if (isalpha (words[row][col]))
    cout << words[row][col];
    if (isspace (words[row][col]))
    cout << endl;
    }


    return 0;
    }


    my output looks something like this:

    This
    is
    the
    text
    I
    want
    my
    program
    to
    sort
    out

    so I now have all my words sorted out in a column, but I now have to copy that array to another array then bubblesort the copied array.

    the copied array should look something like this:

    I
    is
    my
    out
    program
    sort
    text
    the
    This
    want

    but I am completely lost on how to sort a 2d array.

    please please help!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please use code tags for your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    #include <iostream>
    #include <fstream>

    using namespace std;

    int main()
    {
    ifstream myfile;

    myfile.open("/Users/Ashley/Desktop/words.txt"); //this is the text file that hold a sentence

    char words[20][10]; //the array to hold the sorted sentence
    char wordscopy[20][10]; //copy of the previous array that will then be bubblesorted

    int row;
    int col;

    for (row = 0; row < 20; row++)
    myfile.get(words[row],10); //loop to get the file text into the array

    for(row = 0; row < 20; row++)
    for(col = 0; col < 10; col++)
    {
    if (isalpha (words[row][col]))
    cout << words[row][col]; //outputs the letters until non alpha
    if (isspace (words[row][col]))
    cout << endl; //starts new row when it detects a space
    }



    return 0;
    }

    does that make a little sense?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  2. lost disk
    By Benzakhar in forum Linux Programming
    Replies: 7
    Last Post: 01-11-2004, 06:18 PM
  3. So LOST!!!
    By Drew Vance in forum C Programming
    Replies: 6
    Last Post: 04-25-2003, 05:37 PM
  4. Lost ID number
    By ripper079 in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2002, 12:51 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 03:19 PM