简体   繁体   中英

My for loop keeps looping and causes the program to crash

I'm trying to make a program that will receive a string and output an uppercase version of that. My code works, however once it loops through the string and changes it, it immediately crashes and I'm not completely sure why. Here are my two pieces of code.

/*This program is to intended to receive a string and return a version of it in all upper case*/    
 #include <iostream>
 #include <string>
 #include <locale>
 using namespace std;

 string toUpper ( string str)
  {     
    cout <<"\n";    //Puts spaces between the input and output

    for (int i=0; i<str.length(); i++;)
    std::cout << std::toupper(str[i]);   //A loop which goes through each digit of the    string
    break;

    cout <<"\n\n";   //Creates spaces after the output
    return str;
  }

/*This program calls a function to make a string in upper case*/
 #include <iostream>
 #include <string>
 #include <sstream>
 #include <locale>
 #include "toUpper1.h" //Calls the header file which contains the loop      
 using namespace std; 

int main () 
 {  
    cout<<"\nPlease type in a word\n\n";

    string input;   //Creates a variable of cin that can be used in the toUpper command 
    cin>>input; //Makes a user input command part of the declared variable 

    cout<<toUpper(input);       //The command that causes the user input string to be transformed into upper case   
    return 0;   
 }

You can make string to uppercase using code bellow

Boost string algorithms:

#include <boost/algorithm/string.hpp>
#include <string>

std::string str = "Hello World";

boost::to_upper(str);

std::string newstr = boost::to_upper_copy("Hello World");

Or use like this

#include <algorithm>
#include <string>

std::string str = "Hello World";
std::transform(str.begin(), str.end(),str.begin(), ::toupper);

You are breaking the function without returning anything. Use {} to close for loops if you want to use break

prog.cpp:16:5: error: break statement not within loop or switch break;

Also your for loop has an extra ; at the end.

std::cout and std::toupper are useless as you are already including namespace std; and why are you using break; ? there is no need of it. just write

for (int i=0; i<str.length(); i++)
    cout << toupper(str[i]); 

Remove break;

You are not transforming the string, you are outputting its transformation in the function.

instead of

std::cout << std::toupper(str[i]);

use

str[i]=std::toupper(str[i]);

And move all printing out of the function. Changing the string doesn't include printing!

Notice the @bbdude95 answer, too.

edit

Instead of

cout<<"\nPlease type in a word\n\n";
string input;   //Creates a variable of cin that can be used in the toUpper command 
cin>>input;

use

char input[256]; 
cout << "Please type in a word:\n>";
cin.getline( input, 256, '\n' );
 #include <iostream>
 #include <string>
 #include <sstream>
 using namespace std; 


 string toUpper ( string str)
  {     
    cout <<"\n";    //Puts spaces between the input and output

    for (int i=0; i<str.length(); i++)
        str[i] = std::toupper(str[i]);   //A loop which goes through each digit of the    string
    //break;

    cout <<"\n\n";   //Creates spaces after the output
  return str;
  }

int main () 
 {  
    cout<<"\nPlease type in a word\n\n";

    string input;   //Creates a variable of cin that can be used in the toUpper command 
    cin>>input; //Makes a user input command part of the declared variable 

           //The command that causes the user input string to be transformed into uppe case   

    cout << toUpper(input);

   cout << std::endl << "The original string is" << input << std::endl;

   return 0;   
 }

EDIT: Note that keeping the function signature as above ( string toUpper ( string str) , as were required), we are making some extra string copies, and, most important: we are NOT modifying the original string (excute the code and see the result of last cout .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM