Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么在输入的第一行输入测试用例的数量后,我会得到一个垃圾值?_C++_Debugging_Garbage - Fatal编程技术网

C++ 为什么在输入的第一行输入测试用例的数量后,我会得到一个垃圾值?

C++ 为什么在输入的第一行输入测试用例的数量后,我会得到一个垃圾值?,c++,debugging,garbage,C++,Debugging,Garbage,输入: 输入的第一行包含一个整数T,表示 测试用例 每个测试用例的唯一一行包含测试用例的空间分隔部分 名字 输出 对于每种情况,输出格式正确的名称 限制条件: 1.≤ T≤ 100 二,≤ 名称每个部分的长度≤ 10 名称的每个部分都包含英文大写字母和小写字母 字母表,即从“a”到“z”或从“a”到“z” ***Input*** 3 gandhi mahatama gandhi mohandas karamchand gandhi ***Expected Output:*** Gandhi

输入:

输入的第一行包含一个整数T,表示 测试用例

每个测试用例的唯一一行包含测试用例的空间分隔部分 名字

输出

对于每种情况,输出格式正确的名称

限制条件:

1.≤ T≤ 100 二,≤ 名称每个部分的长度≤ 10 名称的每个部分都包含英文大写字母和小写字母 字母表,即从“a”到“z”或从“a”到“z”

***Input***
3
gandhi
mahatama gandhi
mohandas karamchand gandhi


***Expected Output:***
Gandhi
M.Gandhi
M.K.Gandhi


***My output:***
à
Gandhi
M.Gandhi
M.K.Gandhi
为什么我在第一行中得到了a符号? 我创建了一个函数格式化程序来为测试用例提供for循环

如果代码太笨重,我提前道歉

#include<bits/stdc++.h>
using namespace std;
int z;
void formatter(int z){
  for(int q = 0;q <= z;q++){
  string s;
  int count=0;
  int k = 0,p = 0,r = 0,t = 0,l = 0,a = 0,x = 0 ;
  getline(cin,s);
  for(int p = 0;p <= s.size();p++){
    if((s[p]==' ')||(p==0)){
       count++;
       }
  }
  if(count == 1){
    while(r<=s.size()){
    s[0] = s[0] -'a' + 'A';
        cout << s[r] ;
    r++;
  }
   cout << "\n";
  }
  if(count == 2){
    for(int l = 0;l<s.size();l++){
          if(l==0){
             s[l] = s[l] -'a' + 'A';
             cout << s[l] << ".";
             a++;
          }
          if(s[l]==' '){
          if(a==1){
            ++l;
            while(l <= s.size()){
            if(x == 0){
            s[l] = s[l] -'a' + 'A';
          }
            cout << s[l] ;
            a++;
            l++;
            x++;
          }
    }
  }
 }
            cout << "\n";
}
  if(count==3){
  for(int i=0;i<s.size();i++){
        if(i==0){
                s[i] = s[i] -'a' + 'A';
        cout << s[i] << ".";
        t++;
        }
    if(s[i]==' '){
    if(t==1){
        ++i;
        s[i] = s[i] -'a' + 'A';
        cout << s[i] << ".";
        t++;
    }
    else
    while(k<=10){
    if(k==1){
    s[i] = s[i] -'a' + 'A';
    }
        cout << s[i];
        i++;
        k++;
    }
    }


  }
}


}
 cout << "\n";
}
int main(){
  cin >> z;
  formatter(z);
 }
几件事

读取main中的计数的cin不使用结束行 格式化程序中的循环从0到在main中读取的数字(包括0)。这是错误的。 考虑Toupver转换为Uppple的情况。 考虑分解处理1, 2个和3个名称的代码。
这是因为您不知道如何使用调试器一步一行地检查代码,并在每一步检查所有变量的值,以查看代码的逻辑如何工作,这与您预期的结果不同。您还将>>与std::getline混合使用,后者会触发代码中的所有错误。使用你的调试器。我不能这样做,因为我需要读取字符串输入中的空格,而cin不会这样做…所以使用getline你能建议一个替代方法吗?@SamVarshavchikYes,仅使用std::getline来读取每一行输入。这就是它的目的。阅读一行文字。这不是>>所做的。使用>>并期望它读取一行文本是行不通的。但是如果不知道如何使用调试器,您仍然会有bug。除此之外别无选择。知道如何使用调试器是每个C++开发者所需的技能。我知道如何使用调试器。问题是调试器不能指出为什么我在第一行中得到A。它只是在循环中移动而没有问题。无论如何,谢谢你们的帮助…