我的for循环继续循环并导致程序崩溃

时间:2014-02-03 21:11:57

标签: c++ for-loop crash

我试图创建一个接收字符串并输出大写字母的程序。我的代码工作正常,但是一旦它循环遍历字符串并更改它,它会立即崩溃,我不完全确定原因。这是我的两段代码。

/*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;   
 }

5 个答案:

答案 0 :(得分:0)

您可以使用以下代码

将字符串设为大写

提升字符串算法:

#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");

或者像这样使用

#include <algorithm>
#include <string>

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

答案 1 :(得分:0)

您正在破坏该功能而不返回任何内容。如果要使用break

,请使用{}关闭for循环
  

prog.cpp:16:5:错误:break语句不在循环或开关内        打破;

你的for循环也有额外的;最后。

答案 2 :(得分:0)

std::coutstd::toupper没用,因为您已经包含namespace std; 你为什么要使用break;?没有必要。 写一下

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

删除休息;

答案 3 :(得分:0)

您没有变换字符串,而是在函数中输出其变换。

而不是

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

使用

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

将所有打印移出功能。更改字符串不包括打印!

请注意@ bbdude95的答案。

修改

而不是

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;

使用

char input[256]; 
cout << "Please type in a word:\n>";
cin.getline( input, 256, '\n' );

答案 4 :(得分:0)

 #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;   
 }

编辑:请注意保持上面的函数签名(string toUpper ( string str),如果需要),我们正在制作一些额外的字符串副本,最重要的是:我们不修改原始字符串(执行代码和查看最后cout的结果。