Thread: strings and fucntions

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    strings and fucntions

    I have to make a program that asks the user for 4 cities and then asks them if they want to display the 4 cities in the order they were typed in or in the reverse order. We have to have a getCities() function, a getChoice() function, and a displayInOrder() function.

    getCities has to get the four city names from the user.
    getChoice has to get whether or not to display in order or reverse.
    displayInOrder is the display function.

    We also have to put the option of letting the user do the program again.

    When I check the output it gives me nothing so please take a look at it and get back to me.

    This is the code..

    insert
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
        void getCities(int CITIES, string city[]);
        void getChoice(char choice);
        system("pause");
        return 0;
    }
    
    
    void getCities(int CITIES, string city[])
    {
        for (int i = 0; i < CITIES; i++)
        {                
            int j = 0;
            j = i + 1;
            cout << "Enter city number " << j << ".\n";
            getline(cin, city[i]);
        }
        
    }
    
    
    void getChoice(char choice)
    {
    
    
        void getCities(int CITIES, string city[]);
        string city[4];
        cin >> choice;
        if (choice == 'o' || choice =='O')
        {
            cout << city[0] << ", " << city[1] << ", " << city[2] << ", " << city[3] << ".\n" ;
        }
        else if (choice == 'r' || choice == 'R')
        {
            cout << city[4] << ", " << city[3] << ", " << city[2] << ", " << city[1] << ".\n" ;
        }
        else 
        {
            cout << "You have put in a wrong input please try again.";
        }
        
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    98
    For one, in main, you never initialize: CITIES, city, or choice.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    so how would i write it, because i want it to do the things in the functions but I can seem to get it to work

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    98
    Code:
    Delete:
    void getCities(int CITIES, string city[]);
    void getChoice(char choice);
    
    Add:
    int CITIES=4; string city[4]={"","","",""};
    getCities(CITIES, city);
    getChoice(city);
    
    Also delete from getChoice:
    void getCities(int CITIES, string city[]);
    string city[4];

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Ok so here is where I am at now.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    	int CITIES=4; 
    	string city[4]={"","","",""};
    	getCities(CITIES, city);
    	getChoice(city);
    	system("pause");
    	return 0;
    }
    
    
    void getCities()
    {
    	const int CITIES = 5;
    	string city[CITIES];
    	for (int i = 0; i < CITIES; i++)
    	{                
    		int j = 0;
    		j = i + 1;
    		cout << "Enter city number " << j << ".\n";
    		getline(cin, city[i]);
    	}
    	
    }
    
    
    void getChoice()
    {
    
    
    	char choice;
    	cout << "Enter O for In Order, enter R for Reverse Order.";
    	cin >> choice;
    	
    }
    
    
    void displayInOrder()
    {
    	
    	if (choice == 'o' || choice =='O')
    	{
    		cout << "" ;
    	}
    	else if (choice == 'r' || choice == 'R')
    	{
    		cout << "" ;
    	}
    	else 
    	{
    		cout << "You have put in a wrong input please try again.\n Enter O for In Order, enter R for Reverse Order.";
    		cin >> choice;
    		if (choice == 'o' || choice =='O')
    		{
    			cout << "" ;
    		}
    		else if (choice == 'r' || choice == 'R')
    		{
    			cout << "" ;
    		}
    	}
    }

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    for the displayinorder I need to put the cout as the array from get cities and it is telling me too many arguments in function call in int main

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    98
    Well, now that you rewrote the whole thing, then forget every thing I wrote previously.
    Google "C++ function arguments"

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    13
    For one thing you should put prototypes of the functions above main or just place the functions above main otherwise you'll get errors from that. You also set up getCities and getChoice to take no arguments yet in main you are giving it arguments to use (which is why you get the error about too many arguments). displayInOrder also has no idea what choice is so you'll need to pass something into the function that choice will be or declare and initialize it in the function.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Could you give me a sample prototype fandango?

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Lines 8 and 9 in your first post are function prototypes. I assume that'll do for a sample?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Now I am just confused the other guy said to get rid of those lines..

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by solankill View Post
    Now I am just confused the other guy said to get rid of those lines..
    Do you know the difference between a function body and a function prototype?

    In C, a function body can NOT contain function prototypes.

    Tim S.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by stahta01 View Post
    Do you know the difference between a function body and a function prototype?

    In C, a function body can NOT contain function prototypes.

    Tim S.
    Yes it can. It's not usually done, but that's exactly why you can't use parenthesis to default construct objects.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by solankill View Post
    Now I am just confused the other guy said to get rid of those lines..
    Yeah, that was incorrect. You need the prototypes, in addition to the call. Usually though you would put them in a header or at the top of the file. When functions are in the same file as main, and only used within that file, it doesn't really matter how you do it as long as the function or prototype appears before they are used.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I'm going to make a suggestion here. You should write code that's easy to read and understand, and that starts with variable naming.

    You have this:
    Code:
    int CITIES=4;
    string city[4]={"","","",""};
    Wouldn't it make more sense to have:

    Code:
    const int NUMBER_OF_CITIES=4;
    string cities[NUMBER_OF_CITIES] = {"", "", "", ""};
    
    getCities(NUMBER_OF_CITIES, cities);
    It just reads better and is less confusing than using city to represent an array of cities, which is a collection rather than a singular object.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. Simple question on creating fucntions
    By live4soccer7 in forum C Programming
    Replies: 22
    Last Post: 02-08-2009, 07:55 PM
  3. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  4. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  5. declair fucntions before main()?
    By Munkey01 in forum C++ Programming
    Replies: 22
    Last Post: 01-04-2003, 03:38 PM

Tags for this Thread