我想从键盘上获取n行并将它们放入一个数组中...这是行不通的!!! [英] i want to get n lines from keyboard and put them into an array...it does not work!!!

查看:61
本文介绍了我想从键盘上获取n行并将它们放入一个数组中...这是行不通的!!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <map>

using namespace std;

int jomleTokalame(string *a,string c,int count){
    string cc="";
    count =0;
    for(int i = 0 ; c[i]!='\0';i++){
        cc+=c[i];
        if (c[i]==' ' || c[i+1]=='\0'){
            a[count]=cc;
            count++;
            cc="";
        }
    }
    return count;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int location[100];
    string str[100];
    string a[100];
    string s;
    int n,i=1,j,k=0,g;
    cout<<"plz enter number of lines:\n";
    cin>>n;
    for(i=1;i<=n;i++){
        getline(cin,a[i],'\n');
        s=a[i];
        g=jomleTokalame(str,s,0);
        for(j=0;j<=g;j++)
            cout<<str[j];
    }
    getch();
    return 0;
}

推荐答案

此代码已无法修复,几乎每一行都没有使人对语言和/或编程产生深深的困惑.应该删除所有代码并编写清晰的代码.

让我们开始吧:

无需将std包含项与C-stile I/O包含项混淆; std足够用于此目的.

函数jomleTokalame是不需要的,没有任何意义,也没有任何作用,它的名称看起来很隐秘,而不是描述性的.
该函数的参数count的值未使用(分配给0,在调用中也分配了0,未使用),应将其替换为局部变量. (但是,不需要整个计算,因为通过string.length()知道长度的类.)通过指针传递第一个字符串参数是不好的,通过引用传递参数更合适(但是,这没有任何意义,因为函数是现在使用;可以分配字符串).此函数最大的错误是使用空字符.类型string不使用此技术(这本身是非常糟糕的).

在函数_tmain中,未使用变量location,其他参数没有意义,因为没有数组将元素分配给从流读取的字符串.此函数中的外循环跳过a[0],就好像它使用基于1的数组寻址一样.不推荐使用功能getch ...

我认为我不需要继续.

应该怎么办?

使用类型vector<string></string>(包括<vector>),将其实例初始化为n的长度,将循环0写入n-1,使用循环变量作为矢量索引;在循环中,读取一个字符串并将其值分配给循环变量索引的数组元素.如果不少的话,将需要大约10行代码.就这样.

—SA
This code is beyond repair, almost every line makes no sense of shows deep confusion about language and/or programming. All code should be removed and clear code written.

Let''s start:

There is not need to mix up std includes with C-stile I/O includes; std is more then enough for the purpose.

The function jomleTokalame is not needed and makes no sense, it serves no purpose, its name look cryptic rather then descriptive.
The value of parameter count of this function is not used (assigned to 0, in call also assigned with 0 which is not used), should be replaced with local variable. (However, whole calculation is not needed because class the length in known via string.length().) Passing the first string parameter via pointer is bad, passing a parameter by reference is more adequate (however, it makes no sense, because function is of now use; the strings can be assigned). Biggest mistake in this function is using null character. The type string does not use this technique (which is itself extremely bad).

In the function _tmain, variable location is not used, other parameters make no sense, because there is not an array which elements are assigned to the strings read from the stream. The outer loop in this function skips a[0] as if it used 1-based array addressing. The function getch is deprecated…

I don''t think I need to continue.

What should be done?

Use type vector<string></string> (include <vector>), initialize its instance to the length of n, write a loop 0 to n-1, use a loop variable as a vector index; in the loop, read a string and assign its value to the array element indexed by the loop variable. It all will take some 10 lines of code if not less. That''s it.

—SA


让我首先指出您的代码出了什么问题:
Let me start by pointing out what is wrong with your code:
#include "stdafx.h"
#include <iostream>
#include <fstream> //this is for file I/O. you aren''t using files.
#include <conio.h>
#include <string>
#include <stdio.h> //You don''t need this. you are using std:: functions instead for printing and reading to the console.
#include <stdlib.h> //You dont need this either. not sure why you thought you did.
#include <map> //You arent using the std::map. Why are you including this?

//be careful when using "using namespace". This can only be used to use a single namespace.
//you should instead use:
//using std::cin;
//using std::cout;
//...
using namespace std;

//When passing in the parameter c another copy is made of the string.
//You are creating 2 copies of the string when you only need the original.
//Why are you passing in count. The first thing you do is set a copy of the value to 0.
int jomleTokalame(string *a,string c,int count) {
    string cc="";
    count =0;
    for(int i = 0 ; c[i]!=''\0'';i++){
        cc+=c[i];
        if (c[i]=='' '' || c[i+1]==''\0''){
            a[count]=cc; //Yet another copy of a string.
            count++;
            cc="";
        }
    }
    return count;
}

//here you are using the typed main function, which will use whichever character set you are compiling with.
//All your std::strings are are ASCII
int _tmain(int argc, _TCHAR* argv[]) {
    //You have limited space on the stack.
    //You should avoid putting large arrays on the stack, or arrays of large data types.
    int location[100]; //unused variable taking 400 bytes on the stack. Use it or get rid of it
    string str[100]; //Each std::string typically takes 12 bytes. This is taking 1200 bytes on the stack
    string a[100]; //again, this is taking 1200 bytes on the stack
    string s;
    int n,i=1,j,k=0,g; //You should change more meaningful variable names. Unused variable k. Initial value of i is never used.
    cout<<"plz enter number of lines:\n";
    cin>>n; //what if the user enters 
    for(i=1;i<=n;i++) { //I think you are wanting to start with for(i=0;i<n;i++).
        //Array indices start at 0 and go to N-1.
        getline(cin,a[i],''\n'');
        s=a[i]; //Note: this COPIES the value of a[i]. This is slow and unnecessary.
        g=jomleTokalame(str,s,0);
        for(j=0;j<=g;j++)
            cout<<str[j];
    }
    getch(); //This function is deprecated, you should instead use _getch();
    return 0;
}



现在,您知道出了什么问题.让我们看一个更好的相同解决方案:



Now that you know what is wrong. Lets look at the same solution that is much better:

#include <iostream>
#include <conio.h>
#include <string>

using std::cout;
using std::cin;
using std::string;

//Now we will pass parameter strLine by const reference
//This does not create a copy, but does say that we can''t change the original
int jomleTokalame(string *strWords, const string &strLine) {
    //instead of using a variable cc, we will just modify the array directly. This will avoid copying.
    int nWords = 0;
    strWords[nWords]="";
    for(int nChar = 0; strLine[nChar] != ''\0''; ++nChar){//++X is often (marginally) faster than X++
        strWords[nWords] += strLine[nChar];
        if (strLine[nChar] == '' '' || strLine[nChar + 1] == ''\0''){
            //no copying to be done now.
            strWords[++nWords]=""; //Make sure the next string is empty.
        }
    }
    return nWords + 1; //This is a better place to increment the number of words
}

int _tmain(int argc, _TCHAR* argv[]) {
    string strWords[100]; //you should really use something like a std::vector for this, but i will let you do that.
    string *strLines = NULL; //This will be allocated dynamically once we know how big to make the array.
    int nLines, nLine, nWords, nWord;
    cout << "plz enter number of lines:\n";
    cin >> nLines;
    strLines = new string[nLines]; //This way you won''t get a buffer overflow if nLines>100 and it doesn''t take up precious space on the stack.
    for (nLine = 0; nLine < nLines; ++nLine) {//++X is often (marginally) faster than X++
        getline(cin, strLines[nLine], ''\n'');
        nWords = jomleTokalame(strWords, strLines[nLine]); //pass a[nLine] in directly
        for (nWord = 0; nWord < nWords; ++nWord)
            cout << strWords[nWord];
    }
    delete[] strLines; //delete the dynamically allocated array of strings
    _getch();
    return 0;
}


这篇关于我想从键盘上获取n行并将它们放入一个数组中...这是行不通的!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆