Need help for class project

This is a discussion on Need help for class project within the C++ Programming forums, part of the General Programming Boards category; the project ask the user to enter a string and then prompts the user to enter a character. the program ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Unhappy Need help for class project

    the project ask the user to enter a string and then prompts the user to enter a character. the program should display the location of the first occurrence of the character in the string.

    for example: the user enters the string "abcde", and the character "d", the program should say the character d is character number 4 in the string.

    Code:
    #include <iostream.h>
    
    
    
    int main()
    {
    	char line[81];
    	char fat[81];
    	char* ch_ptr = line;
    	char* ch_ftr = fat;
    	int count = 0;
    
    	cout << "\nEnter a string of characters:\n\n";
    	cin.getline(line, 81);
    
    	cout << "\nEnter one character in the previous string\n\n  ";
    	cin.getline(fat, 81);
    
    
    	
    
    	while (*ch_ptr == 'fat')
    	{
    		++count;
    		++ch_ptr;
    	}
    
    	cout << "\nThe string you entered is in the  " <<  count << "space .";
    
    	return 0;
    }
    okay i got it to count the number of characters but i have no idea how to locate the second character and display the location, if anybody can guide me to the proper way of coding , i would greatly appreciate it.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Another solution is a string object.

    Code:
    std::getline(cin, stringObject);
    cin >> oneChar;
    
    size_t pos = stringObject.find(oneChar);
    
    // pos is the position of the character.
    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 08:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM

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