toupper Function Problems!

This is a discussion on toupper Function Problems! within the C++ Programming forums, part of the General Programming Boards category; Hey Guys, I asked this question before but I can't figure it out. string Articles [5] = {"a", "the" ,"one" ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    44

    Exclamation toupper Function Problems!

    Hey Guys,

    I asked this question before but I can't figure it out.

    string Articles [5] = {"a", "the" ,"one" , "some" ,"any" };
    string Nouns [5] = {"man", "tree" ,"dog" , "car" ,"town" };
    string Verbs [5] = {"drove","jumped", "ran" ,"walked" , "threw"};
    string Prepositions [5] ={"to","from", "over" ,"under" , "through"};

    example I'm using a ramdon number to display different sencentes but how could I use toupper to make the first character of the articles a capital letter

    I have something like this: Sentence(num) is just a function that generates a random number.
    Code:
       	for ( int i = 0; i <= number_lines; i++)
     	{
    	
      	cout  << "    " << Articles[Sentence(num)] << " " << Nouns[Sentence(num)]    
          		 << " " << Verbs [Sentence(num)]   << " " << Prepositions[Sentence(num)]
      		     << " " << Articles[Sentence(num)] << " " << Nouns[Sentence(num)] << ".\n";
      	}
    The first character of Article[Sentence(num)] should be a capital letter but the next Article just be lower case .

    so I can have something like

    A car crashed to one tree


    When I try to access the first character on the string I use

    Code:
    char ch = Articles[Sentence(num)][0]
    ch = toupper(ch)
    how could I copy the first character to after a make it a capital letter?

    Please some help
    Thank you
    Dilmer Valecillos
    I already read the tutorials in this website please give me some other help !

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Try this.
    Code:
    Articles[Sentence(num)][0]=toupper(Articles[Sentence(num)][0]);

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> char ch = Articles[Sentence(num)][0]
    That code makes a copy of the character and stores it in ch, so you are making the copy uppercase instead of the actual character in the string.

    Quantum1024's method makes the most sense, but you could also make ch a reference so that it referred to the actual character instead of a copy of it.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    ok if I use Articles[0][0]=toupper(Articles[0][0]); // This will access the character a and make it a upper case letter.
    then My problem is, when I display the info on the screen How can I add the uppercase letter to my original array?
    In this case I'm accessing the first word included in each array.

    cout << " "<< Articles[0] << " " << Nouns[0]
    << " " << Verbs [0] << " " << Prepositions[0]
    << " " << Articles[0] << " " << Nouns[0] << ".\n";

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    The sentences should begin with a capital letter and end with a period.
    scroll all the way down to void Create_Story() // Option 4 Thats where I'm trying to make the first letter a capital letter.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cctype> //Using (toupper) function to convert low case letter to upper case
    #include "sentence.h"//adding a header file to the program
    
    using namespace std;
    
    void Word_List();
    void Change_Words();
    void Write_Sentence();
    void Create_Story();
    int Sentence(int x);// Function prothotype.
     
    //Array declarations as global variables
    string Articles [5] = {"a", "the" ,"one" , "some" ,"any" }; 
    string Nouns [5] = {"man", "tree" ,"dog" , "car" ,"town" };
    string Verbs [5] = {"drove","jumped", "ran" ,"walked" , "threw"};
    string Prepositions [5] = {"to","from", "over" ,"under" , "through"};
    int i, x, Number;
    string Replace;
    
    
    int main()
    {
    	char selection;
    
    		do
    		{
    	
    			system("cls");
    			cout << "     Story simulation Program [[ Dilmer Valecillos CS1600-001 ]] " << endl; 
    			cout << "  \n      Main Menu\n"
    					<< "      ---- ----\n"
           				<< " 1 <- Show word lists \n"
    					<< " 2 <- Change words \n"
    					<< " 3 <- Write a sentence \n"
    					<< " 4 <- Write a story \n"
    					<< " 5 <- Quit\n\n"
    					<< "      Choice: ";
    
      			//Get menu selection
      			selection = cin.get();
    
      		//Process selection
      		switch( selection)
      		{
      	  		case '\n':	
    				break;
    	 	 	case '1': Word_List(); 
    				break;
           	 	case '2': Change_Words(); 
    				break;	  
    	 	 	case '3': Write_Sentence();	
    				break;
         	 	case '4': Create_Story();  
    				break;
          	 	case '5':	              				 
    				break;
          	 	default: cout << '\a';
      		}
    
        }
    		while( selection != '5' );
    
    		return 0;
    }
    
    
    void Word_List() //Option 1
    {
    	system("cls"); 
    	char selection;
    	
    		do	
    		{
    
    			system("cls");  // Clear the screen
    			cout <<"\n\n      Word Lists\n\n"
    					<<" 1 <- Article\n"
    					<<" 2 <- Nouns\n"
    				    <<" 3 <- Verbs\n"
    				    <<" 4 <- Prepositions\n"
    				    <<" 5 <- Return to Main Menu\n\n"
    				    << "      Choice : ";
    		
    			cin.get(selection);
    
    			switch (selection)
    			{
    
    				case '1': 
    					system("cls");	
    					cout << "\n\n";
    					cout << " Articles\n";
    
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". " << Articles [i] << endl;
    					}
    					system("pause");
    					break;
    					
    				case '2':
    					system("cls");
    
    					cout << "\n\n";
    					cout << " Nouns\n";
    
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". " << Nouns [i] << endl;
    					}
    					system("pause");
    					break;
    				case '3':
        	  	  		system("cls");
    
    					cout << "\n\n";
    					cout << " Verbs\n";
    	
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". "  << Verbs [i] << endl;
    					}
    					system("pause");
    					break;
    				case '4':
    					system("cls");
    					cout << "\n\n";
    					cout << " Prepositions\n";
    
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". " << Prepositions [i] << endl;
    					}
    					system("pause");							
    					break;
    				case '5':	
    					system("cls");
    					break;	
    				case '\n':
    					break;
    				default: cout << '\a';
    			}
    		}
    		while( selection != '5');
    }
    
    void Change_Words()//Option 2
    {
    	system("cls");
    	char selection;
    	
    		do	
    		{		
    
    			system("cls");  // Clear the screen
    			cout<<"\n\n   Change words\n\n"
    				   <<" 1 <- Article\n"
    				   <<" 2 <- Nouns\n"
    			  	   <<" 3 <- Verbs\n"
    		           <<" 4 <- Prepositions\n"
    		           <<" 5 <- Return to Main Menu\n\n"
                       <<"      Choice : ";
    	
    			cin.get(selection);
    
    			switch (selection)
    			{
    
    				case '1':// Articles			
    					system("cls");	
    					cout << "\n\n";
    					cout << " Articles\n";
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". " << Articles[i] << endl;
    					}
    					cout << " Number :";
    					cin  >>  Number;
    
    					i = Number;
    					Articles[i];
    
    					cout <<" Replace with: ";
    					cin  >> Replace;
    
    					Articles[Number] = Replace;
    		
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". "<< Articles[i] << endl;
    					}
    					system("pause");
    	
    					break;
    				case '2': // Nouns
    					system("cls");
    
    					cout << "\n\n";
    					cout << " Nouns\n";
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". " << Nouns[i] << endl;
    					}
    					cout << " Number :";
    					cin  >>  Number;
    
    					i = Number;
    					Nouns[i];
    					cout <<" Replace with: ";
    					cin  >> Replace;
    
    					Nouns[Number] = Replace;
    		
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". "<< Nouns[i] << endl;
    					}
    					system("pause");
    		
    					break;
    				case '3': // Verbs 
    					system("cls");
    
    					cout << "\n\n";
    					cout << " Verbs\n";
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". " << Verbs[i] << endl;
    					}
    					cout << " Number :";
    					cin  >>  Number;
    		
    					i = Number;
    					Verbs[i];
    
    					cout <<" Replace with: ";
    					cin  >> Replace;
    			
    					Verbs[Number] = Replace;
    		
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". "<< Verbs[i] << endl;
    					}
    					system("pause");
    					break;
    				case '4':
    	 				system("cls");
    
    					cout << "\n\n";
    					cout << " Prepositions\n";
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". " << Prepositions[i] << endl;
    					}
    					cout << " Number :";
    					cin  >>  Number;
    				
    					i = Number;
    					Prepositions[i];
    
    					cout <<" Replace with: ";
    					cin  >> Replace;
    					cout <<" updated list: " << endl;
    			
    					Prepositions[Number] = Replace;
    		
    					for (i = 0; i < 5; i++)
    					{
    						cout << " " << i << ". "<< Prepositions[i] << endl;
    					}
    
    					system("pause");
    							
    					break;
    				case '\n':
    					break;		
    					case '5':
    					system("cls");
    					break;
    				default: 
    					cout << '\a';
    			}
      		}
    		while( selection != '5');
    }
    
    void Write_Sentence()//Option 3
    {
    	system("cls");
      	int num;
      	cout << "\n\n Here's the sentence:\n\n ";
      	cout << "Sentence -> " << Articles[Sentence(num)] << " " << Nouns[Sentence(num)]//Function call 
    		    << " " << Verbs [Sentence(num)] << " " << Prepositions[Sentence(num)]
    		    << " " << Articles[Sentence(num)] << " " << Nouns[Sentence(num)] << endl;
    		//The line above display a sentence in the order of article,
            //noun, verb, preposition, article, noun
    
      	cout << endl; 
    	system("pause");
    }
    
    void Create_Story() // Option 4
    {
      	system("cls");
    	int number_lines;
    	int num;
    
    	cout << "\n\n    How many lines are there in the story?\n"
    	        << "    Lines :  ";
    	cin   >> number_lines; 
      	cout << "\n    Here's the story :\n"
    		    <<"    -------------------\n";
    		
    	//string Articles [5] = {"a", "the" ,"one" , "some" ,"any" }; 
      	
    	for ( int j = 0; j <= number_lines; j ++)
    	{
    	char ch = toupper(Articles[0][0]);
    	cout << ch;
    
    	
      	cout  << "    " << Articles[Sentence(num)] << " " << Nouns[Sentence(num)]    
          		 << " " << Verbs [Sentence(num)]   << " " << Prepositions[Sentence(num)]
      		     << " " << Articles[Sentence(num)] << " " << Nouns[Sentence(num)] << ".\n";
      	 
       }
     	system("pause");
    }

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Is this what you want:
    Code:
    string Articles [5] = {"a", "the" ,"one" , "some" ,"any" };
    string Nouns [5] = {"man", "tree" ,"dog" , "car" ,"town" };
    string Verbs [5] = {"drove","jumped", "ran" ,"walked" , "threw"};
    string Prepositions [5] ={"to","from", "over" ,"under" , "through"};
    
    string mySentence = Articles[0] + " " + Nouns[0] + " " + Verbs[0] +
    		".";
    mySentence[0] = toupper(mySentence[0]);
    
    cout<<mySentence<<endl;

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    Thanks a lot it worked

    Code:
    	for ( int j = 0; j <= number_lines; j ++)
    	{
    		
    	   string mySentence = Articles[Sentence(num)] + " " + Nouns[Sentence(num)] + " " 
    										+ Verbs[Sentence(num)] + " " + Prepositions[Sentence(num)] + 
    		                                  " " +  Articles[Sentence(num)] + " " + Nouns[Sentence(num)] + ".";
          
    	   mySentence[0] = toupper(mySentence[0]);
    	   cout << mySentence << endl;
    
    		//cout  << " " << Articles[Sentence(num)] << " " << Nouns[Sentence(num)]    
          		//     << " " << Verbs [Sentence(num)]   << " " << Prepositions[Sentence(num)]
      		        // << " " << Articles[Sentence(num)] << " " << Nouns[Sentence(num)] << ".\n";
    
    
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21