Can you help with a for loop I am working on.....it is a mess...

This is a discussion on Can you help with a for loop I am working on.....it is a mess... within the C++ Programming forums, part of the General Programming Boards category; I am having trouble implemting a function that I want to search a list and then replace a name in ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Can you help with a for loop I am working on.....it is a mess...

    I am having trouble implemting a function that I want to search a list and then replace a name in that list. What I have so far is so wrong at this point that I am lost on were to even start with correcting it...
    Code:
    void Controller::ReplaceName()
    {
    	string find;
    	string replace;
    	int input;
    
    	cout << "Please enter the name you want to replace : " << flush;
    	cin >> find;
    	cout << endl << "Please enter the name you want to replace it with : " << flush;
    	cin >> replace;
    
    	list<Artist>::iterator iter ;
    
    		for(iter = myArtist.begin(); iter != myArtist.end(); iter++)
    		{
    			if ((*iter).getArtBandName() == find)
    			{
    			cout << "Would you like to replace" << (*iter).getArtBandName() << " with " << replace << endl;
    			cout << "Type 1 for Yes or 2 For No : " << flush;
    			cin >> input;
    				if (input == 1)
    				{
    					*iter = replace;
    				}
    				else if (input == 2)
    				{
    					cout << "Returning to Main Menu" << endl;
    				}
    				else
    				{
    					cout << "Invalid input : Returning to Main Menu" << endl;
    				}
    			}
    		}			
    }
    It compiles but stops doing what I am asking it do at the seond if loop. From what I can tell my main areas of concern are my second nested if loop (can this even be done) and my the body of "if(input == 1).

    Should I nest my if/else loop in a diffrent type of loop?

    I am really confused on how I actually make the name change offical. Am I even on the correct track or should I just start from scratch?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > *iter = replace;
    Unless you have the = operator overloaded, it seems like here you'd instead want:
    Code:
    					*iter = setArtBandName(replace);

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    16
    im not sure if you have typo in your post or what... but there is no such thing as a nested if loop. There are nested for, and while loops, but no nested if loop. an if statment will execute once if the condition is true and skip it if false. What you have are nested if statements. if you already knew this then, i would check the conditions, to make sure they and entering and exiting when they should be. since you are using pointers and other things more complicated than if statements, im guessing you already knew what i just said.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by swoopy
    > *iter = replace;
    Unless you have the = operator overloaded, it seems like here you'd instead want:
    Code:
    					*iter = setArtBandName(replace);

    Yeah thats what I was thinking but I can't see if it works because I have not figured out why my nested loops don't work.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well this:
    > *iter = replace;
    is probably going to blow up your program. Actually I'm surprised it even compiles, as replace is a string, and *iter is an Artist. Maybe you have an operator = member in your Artist class.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by bradleym83
    im not sure if you have typo in your post or what... but there is no such thing as a nested if loop. There are nested for, and while loops, but no nested if loop. an if statment will execute once if the condition is true and skip it if false. What you have are nested if statements. if you already knew this then, i would check the conditions, to make sure they and entering and exiting when they should be. since you are using pointers and other things more complicated than if statements, im guessing you already knew what i just said.
    As shameful as it is to admit, there is no type. Everything I learned is mostly self taught. Which is making me realize I need to slow down before I move on to a new topic. As you can see the "nested if" loop I have up there gives the user an option. Should this option be in another function or is there a diffrent loops sequence I can use to keep it were it is?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by swoopy
    Well this:
    > *iter = replace;
    is probably going to blow up your program. Actually I'm surprised it even compiles, as replace is a string, and *iter is an Artist.
    I am guessing it complies because of my bogus loop sequence that I still have not figured out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 04:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 08:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 05:08 AM

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