簡體   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