简体   繁体   English

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

[英]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: 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");

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 如果要使用break,请使用{}关闭循环

prog.cpp:16:5: error: break statement not within loop or switch break; prog.cpp:16:5:错误:break语句不在循环或switch break中;

Also your for loop has an extra ; 你的for循环还有一个额外的; at the end. 在末尾。

std::cout and std::toupper are useless as you are already including namespace std; std::coutstd::toupper没用,因为您已经包含了namespace std; and why are you using break; 以及为什么要使用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. 也请注意@ bbdude95的答案。

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 . 编辑:请注意,保持函数签名如上( string toUpper ( string str) ,根据需要),我们正在制作一些额外的字符串副本,并且最重要的是:我们不修改原始字符串(执行代码并查看最后一cout结果。

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

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