-2

I have a function (int, string) in Main:

string word("HELLO");
int x = 0;
char choice;
swap(x, word);

I am trying, with no success, to pass into the following function:

void swap(int, string) {

int x = 0;
string word = "HELLO";

cout << "Would you like to change a letter? Please enter the letter 
position. " << endl;
cin >> x;

if (x == 1) {
    cout << "What do you want to change it to?" << endl;
    cin >> word[0];

I keep getting this error:

Error C2664 'void std::swap(std::exception_ptr &,std::exception_ptr &) throw()': cannot convert argument 1 from 'int' to 'std::exception_ptr &'

What gives?

  • 3
    Rename your function from `swap` or remove the obvious `using namespace std` you have – Tas Oct 18 '17 at 04:16
  • 1
    Your code looks hideous and too messy. Your function `swap` doesn't really swap anything only replaces a certain character. Rename it. What do you do after `std::cin >> word[0];` ? – knoxgon Oct 18 '17 at 04:18
  • It's another if/else statement. So the idea if someone press 1 it changes the first letter. 2 changes the second. – Tony Cossio Oct 18 '17 at 04:27
  • 2
    Please post the full code. We aren't wizards, Tony – knoxgon Oct 18 '17 at 04:29
  • Alright, i updated it on top. I made some updates from the suggestions, now my error is 'change':identifier not found. – Tony Cossio Oct 18 '17 at 04:51
  • I take it you can't use switch statements for strings, which is why I put elseif. Maybe I heard wrong. – Tony Cossio Oct 18 '17 at 04:52
  • In c++, the function declaration must be top of the main function. Copy paste the following on the top of the main. `void change(int, string);` – knoxgon Oct 18 '17 at 04:52
  • And yes, you heard it right, you can't use `switch` statement for `std::string`. You can read more here: https://stackoverflow.com/questions/650162/why-switch-statement-cannot-be-applied-on-strings – knoxgon Oct 18 '17 at 04:59
  • Thanks, I actually had the prototype before but I must of deleted it when making the other changes! However I have another problem, it's not actually changing the string. – Tony Cossio Oct 18 '17 at 05:04
  • Yes, it doesn't change it because you are not using any parameters. Use the following function declaration and remove the string declaration inside your function. Use the parameter `word` instead. `void change(string& word);` The reason you are using reference parameter for the string is to manipulate the string inside another local function that will affect your original string. – knoxgon Oct 18 '17 at 05:08
  • @VG You are rewriting the code in comments. Comments are meant to fix flaws in the question, not to answer them. You should instead answer down below with all necessary information where it can be read easily. – Passer By Oct 18 '17 at 05:16
  • Thanks, everything is working like a champ now. – Tony Cossio Oct 18 '17 at 05:27
  • @V G was a tremendous help, everything works. – Tony Cossio Oct 18 '17 at 05:28
  • @TonyCossio I'm very happy about it. – knoxgon Oct 18 '17 at 05:35
  • @PasserBy Yes I was thinking about it but since the lad is showing great interest in learning, I wanted to see if he could follow the instructions from comments. – knoxgon Oct 18 '17 at 05:43
  • @TonyCossio Please read my updated answer! You will observe some significant changes that sure will help you in the future. – knoxgon Oct 18 '17 at 08:23

1 Answers1

0

The main problem with your code is the indentation. Your code is not readable and mostly hard to comprehend it. Beautify it. Write nice, readable and a structured code. You can read more about indentation at the following link.

https://en.wikipedia.org/wiki/Indentation_style

The next thing is the function declaration. You do not declare your function before defining it. The function declaration should be top of the main function and definition of the function should be below the main function. You can find more info about the function declaration at the following link:

http://en.cppreference.com/w/cpp/language/function

Since you are not using a char array to print out the string, it is useless to go through the string with a loop. Include the <string> library and start to work towards the string type. By passing the string variable inside std::cout is enough to print out the string.

Lastly, since you are trying to manipulate a string variable outside the main function, it is required that you are passing a reference parameter instead.

void myFunction(std::string& parameter);

This way, the original variable that exists inside the main or inside any other function will be altered. Without the reference, &, the value you are trying to modify will not be changed.

The following link demonstrates the use of reference.

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

Please read my comments below of why some changes were applied. I made crafty changes to the change function. You are now eligible to work towards any string type with any size.

#include <iostream>
#include <string> //When you are working on strings, use the string library.

using namespace std;

//Function declaration is very important. Have the declarations above main.
void change(string&);

int main() {
    string word("HELLO");
    char choice;

    cout << "The word is : " << endl;

    cout << word << endl;

    //No need for the for loop to print out the string as
    // we are working on a string and not a char array.
    //  for (int i = 0; i < word.length(); i++) {
    //      cout << word[i];
    //  }


    change(word);

    cout << "The new word is" << endl << word << endl;

    cout << "Would you like to enter another change ? Enter Y or N ? " << endl;

    cin >> choice;

    if (choice == 'y' || choice == 'Y') {
        change(word);
        cout << word << endl;
    }
    else {
        cout << "Good Bye" << endl;
    }

    system("pause");

    return 0;

}


//When your datatype is to be modified outside the function, use the reference
//parameter type '&'.
//Without the reference type, your modified version of the type will only be modified 
//inside that function.
//The original one will not be altered.

void change(string& word) {
   /*
    *  size_t is simply unsigned int, to work towards manipulation and accessing
    *  of string types, use unsigned int or std::size_t
    */
    size_t x = 0;

    cout << "Would you like to change a letter? Please enter the letter position. " << endl;
    cin >> x;

    //Check to see if the inputted value is within the string length range.
    if(x > 0 && x <= word.length())
        cout << "What do you want to change it to?" << endl;
    else{
        cout << "The entered position is outside the string size range\n";
        return; //Quit from the function if the condition is not met.
    }

    /*
     *   Instead of using if/else if statements,
     *   Just make a normal loop. Much simpler.
     */

     for(size_t i = 0; i < word.length(); i++){
         if((x-1) == i)
             cin >> word[i];
     }
}
knoxgon
  • 1,070
  • 2
  • 15
  • 31
  • Answers should be (mostly) self contained, referring to comments doesn't qualify, they may be removed without notice. – Passer By Oct 18 '17 at 06:28
  • @PasserBy Thank you for the warning! – knoxgon Oct 18 '17 at 06:39
  • This is good, my next question was optimization. I did originally have word.length() instead of a hard counter but it threw an error. Not sure what I did wrong, but I do understand utilizing size_t and the array's length as the best option. – Tony Cossio Oct 18 '17 at 12:50
  • The reason can be due to the lack of string library. Because you didn't have it in the far beginning. Or, you may have also forgotten to add the parentheses. `word.length()` as I sometimes do too. – knoxgon Oct 18 '17 at 13:19