Thread: problem with my strings

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    11

    problem with my strings

    My compiler keeps telling me I have an error using the following code (a function declaration at the beginning of the file)...

    Code:
    double showmenu (int menulist, string menuitem, double price);
    it says it expects a ')' right after menulist.

    Then later when I declare my variables, like this

    Code:
    	int menulist [8];
    	string menuitem [8];
    	double order [8], price[8];
    that string is an undefined symbol.....

    I included the "#include <string.h>" header file... I'm just not sure what I'm doing wrong

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    try #include <string> instead of #include <string.h> and don't forget add:
    use namespace std;

    for the function declaration, did you want to pass arrays, if so it'll be like these
    double showmenu (int menulist[], string menuitem[], double price[]);

    if there's error, post the whole code
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    I get the following two errors when I try to use "#include <string>" with the using namespace std; It says "Unable to include file "STRING""(for #include <string>) and "Declaration Syntax Error" (for using namespace std

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <string.h>
    
    
    double getdata (int menulist[], double order[]);
    double showmenu (int menulist[], string menuitem[], double price[]);
    double printcheck (double order[]);
    
    int main ()
    {
    
    	int menulist [8];
    	string menuitem [8];
    	double order [8], price[8];
    
    	showmenu (menulist[8], menuitem[8], price[8]);
    	getdata (menulist[8], order[8]);
    	printcheck (order[8]);
    
    	return 0;
    
    }
    
    double showmenu (int menulist[8], string menuitem[8], double price[8])
    {
    	int k,m,p;
    
    	for(k=0;k<8;k++)
    	{
    		menulist[k]=k+1;
    	}
    
    	menuitem[0]="Plain Egg" ;
    	menuitem[1]="Bacon and Egg";
    	menuitem[2]="Muffin";
    	menuitem[3]="French Toast";
    	menuitem[4]="Fruit Basket";
    	menuitem[5]="Cereal";
    	menuitem[6]="Coffee";
    	menuitem[7]="Tea";
    
    	price[0]=1.45;
    	price[1]=2.45;
    	price[2]=0.99;
    	price[3]=1.99;
    	price[4]=2.49;
    	price[5]=0.69;
    	price[6]=0.50;
    	price[7]=0.75;
    
    	cout<<"Welcome to Johny's Resturant"<<endl;
    	cout<<"----Today's Menu----"<<endl<<endl;
    
    	for (m=0;m<8;m++)
    	{
    		cout<<menulist[m]<<": ";
    
    		for (p=0;p<8;p++)
    		{
    			cout<<menuitem[p];
    		}
    
    		cout<<"$ "<<price[m]<<endl;
    
    	}
    
    	return 0;
    
    }
    this is the code I have so far...

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I could be your compiler. What you have? Download one at www.bloodshed.net
    Change all #include's to look like the following to make it standard compliant
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    and another issue in your main about passing array, you want to pass the variable name of the array without '[]' as well as the variable that contains the size of the array (unless you make the size a global variable)
    Code:
    const int SIZE = 8;
    showmenu (menulist, menuitem, price, SIZE);
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    I now complied and changed it using Dev-C++

    now i get the errors
    "too many arguments to" at lines 6,7,8
    and
    "at this point in file" at lines 18,19,20

    I don't understand what those mean

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std; 
    
    double getdata (int menulist[8], double order[8]);
    double showmenu (int menulist[8], string menuitem[8], double price[8]);
    double printcheck (double order[8]);
    
    int main ()
    {
    
    	int menulist [8];
    	string menuitem [8];
    	double order [8], price[8];
    	const int size = 8;
    	
    	showmenu (menulist, menuitem, price, size);
    	getdata (menulist, order, size);
    	printcheck (order, size);
    
    	return 0;
    
    }
    
    double showmenu (int menulist[8], string menuitem[8], double price[8])
    {
    	int k,m,p;
    
    	for(k=0;k<8;k++)
    	{ 
    		menulist[k]=k+1;
    	}
    
    	menuitem[0]="Plain Egg" ;
    	menuitem[1]="Bacon and Egg";
    	menuitem[2]="Muffin";
    	menuitem[3]="French Toast";
    	menuitem[4]="Fruit Basket";
    	menuitem[5]="Cereal";
    	menuitem[6]="Coffee";
    	menuitem[7]="Tea";
    
    	price[0]=1.45;
    	price[1]=2.45;
    	price[2]=0.99;
    	price[3]=1.99;
    	price[4]=2.49;
    	price[5]=0.69;
    	price[6]=0.50;
    	price[7]=0.75;
    
    	cout<<"Welcome to Johny's Resturant"<<endl;
    	cout<<"----Today's Menu----"<<endl<<endl;
    
    	for (m=0;m<8;m++)
    	{
    		cout<<menulist[m]<<": ";
    
    		for (p=0;p<8;p++)
    		{
    			cout<<menuitem[m];
    		}
    
    		cout<<"$ "<<price[m]<<endl;
    
    	}
    
    	return 0;
    
    }

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    double getdata (int menulist[8], double order[8], int SIZE);
    // fix other function prototypes and definitions
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    I got it to work with your suggestions... thank you soo much for your help!

    but i don't understand why "size" mattered when it was declared elsewhere... is that something thats built into the header files?

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by wiresite
    I got it to work with your suggestions... thank you soo much for your help!

    but i don't understand why "size" mattered when it was declared elsewhere... is that something thats built into the header files?
    I don't really get your question, but..
    when you pass an array you only pass a pointer to the first index of array, that's why you need to pass the size of array to prevent accessing outside array's constraint.
    Another suggestion, change all occurences of '8' to SIZE since you already declared SIZE=8. You might need to re-locate where you declare and initialize SIZE. That way if you need to, you only need to make changes to SIZE and it'll be reflected to all of them that use it
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    I've got another question....

    in my program it asks users to enter choices up to 8 times. They can terminate at anytime before 8 otherwise the program will end itself after the 8th choice. I want the number of choices to be recorded into variable "int d"

    This is the code I use....

    Code:
    cout<<"Do you want to make a selection Y/y (Yes), N/n (No): ";
        cin>>h;
        
        
        for (r=0;r<8;r++)
        {
            switch(h)
                {
                     case 'y':
                     case 'Y': cout<<endl;
                                   cout<<"Enter item number: ";
                                   cin>>orderitem[r];
                                   cout<<endl;
                                   cout<<"Select another item Y/y (Yes), N/n (No): ";
                                   cin>>h;
                                   break;
                     case 'n':
                     case 'N': d=r;
                                   r=10;
                                   break;
                }
            
        
        }
    When they keep selecting "Yes" They are then asked for the item number....That is then stored in the array of "orderitem" If however they select "No" then d will be set to whatever the loop is at (so if its the 4th time through then d=3 (since it started on 0 for array purposes later)....well the problem I'm running into is when I try and pass it back through the main program to another function

    heres how its used in the other function

    Code:
    for (p=0;p<d;p++)
        {
            //listing the items and their prices
            cout<<left<<setw(15)<<menuitem[orderitem[p]]<<price[orderitem[p]]<<endl;
        
        }
    I know the cout part is not the problem but the problem is within my loop...for some reason its not recognizing that d should be larger than -1 (for somereason thats what its coming through as)


    if the full source code is needed lemme know i'll post it...

    I know I can figure this out...I just need a point in the right direction

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Where is d declared? What's it's scope relative to the function that isn't working correctly?

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
     
    const int size = 8;
    
    double getdata (int d, int orderitem[8], int size);
    double printcheck (int d, int orderitem[8], double order[8], int menulist[8], string menuitem [8], double price[8], int size);
    
    int main ()
    {
    
    	int menulist [8], orderitem[8];
    	string menuitem [8];
    	double order [8], price[8];
    	int d;	
      	
    	showmenu (menulist, menuitem, price, size);
    	getdata (d, orderitem, size);
    	printcheck (d, orderitem, order, menulist, menuitem, price, size);
    
        cin.get();
    
        
    
    	return 0;
    
    	
    
    }
    
    double getdata (int d, int orderitem[8], int size)
    {
        char h;
        int r;
        
        cout<<"Do you want to make a selection Y/y (Yes), N/n (No): ";
        cin>>h;
        
        
        for (r=0;r<8;r++)
        {
            switch(h)
                {
                     case 'y':
                     case 'Y': cout<<endl;
                               cout<<"Enter item number: ";
                               cin>>orderitem[r];
                               cout<<endl;
                               cout<<"Select another item Y/y (Yes), N/n (No): ";
                               cin>>h;
                               break;
                     case 'n':
                     case 'N': d=r;
                               r=10;
                               break;
                }
            
        
        }
        
    }
    double printcheck (int d, int orderitem[8], double order[8], int menulist[8], string menuitem [8], double price[8], int size)
    {
        //local variables located below
        int p;
        //start printcheck
        
        cout<<endl;
        cout<<"Welcome to Johny's Resturant"<<endl;
        cout<<d;
        
        for (p=0;p<d;p++)
        {
            //listing the items and their prices
            cout<<left<<setw(15)<<menuitem[orderitem[p]]<<price[orderitem[p]]<<endl;
        
        }
    
    }
    its declared in "int main()"

    its scope is to create the number of times the loop in "printcheck" needs to run

    Program interaction should look like this (just an example)

    Do you want to make a selection Y/y (Yes), N/n (No): y
    Enter item number: 1
    Select another Item Y/y (Yes), N/n (No): y
    Enter item number: 4
    Select another Item Y/y (Yes), N/n (No): y
    Enter item number: 5
    Select another Item Y/y (Yes), N/n (No): n

    *--function getdata ends here... when the user selects N/n the statements in switch assign 'r' to 'd' then assign the value of 10 to r so that the loop cancels (THIS works somewhat as planned... the value of R does become greater than 8 so the loop cancels and d does get assigned the value of '3')

    now whats NOT working is the value of 'd' being passed back through the main function to 'printcheck' because in the function 'printcheck' the value now becomes -1 for some reason...

    (adding cout<<r<<" "<<d; at the end of getdata shows that everything there worked fine...adding cout<<d; to the beginning of printcheck shows d with a value of -1)

  12. #12
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You want to change the value of 'd' in the getdata() therefore you need to pass by reference not the value. You need to make change on getdata() prototype and definition
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM