繁体   English   中英

我的for循环不断循环并导致程序崩溃

[英]My for loop keeps looping and causes the program to 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;   
 }

您可以使用以下代码将字符串转换为大写

Boost字符串算法:

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

您正在中断该函数而未返回任何内容。 如果要使用break,请使用{}关闭循环

prog.cpp:16:5:错误:break语句不在循环或switch break中;

你的for循环还有一个额外的; 在末尾。

std::coutstd::toupper没用,因为您已经包含了namespace std; 以及为什么要使用break; 不需要它。 写吧

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

删除中断;

您不转换字符串,而是在函数中输出其转换。

代替

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' );
 #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结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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