2017-04-02 58 views
-4
#include <iostream> 
using namespace std; 

int main(){ 

string Firstname,Surname; 

cout << "Full name:" <<endl; 
cin >> Firstname >> Surname; 

return 0; 

我想要的命令來工作,如果用戶只是它持續不斷要求他們再次姓有名字,通常如果用戶沒有輸入和再次.....如何讀取文本文件,並存儲成詞二維數組

+2

stackoverflow.com不做作業的網站。告訴我們你的努力或代碼。 –

+1

請至少添加你自己的查看。 1.你想從文件中讀取:Google如何做到這一點! 2.你想將某些東西存儲到二維數組中:Google它! 3.合併這兩個概念。如果再次發生任何錯誤。 – datell

+0

夥計這是我讀取文件的代碼@MohammadTayyab –

回答

0

我已經評論了我的代碼,並創建了一個代碼來從文件中讀取名稱並將它們存儲到二維字符串數組中,只要您想要。

文本文件

James 
Will 
Bruce 
Wayne 
Harry  
Potter 

代碼:

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 
int main() 
{ 
    string str; 
    int count=0; 
    ifstream in("File.txt"); 
    while (!in.eof()) 
    { 
     getline(in, str); 
     count++; //counting names 
    } 
    in.close(); 
    count = count/2; //dividing count by 2 
    //SYNTAX FOR 2D ARRAY 
    string **nameArray = new string*[count]; //making array of count/2 
    for (int i = 0; i < count; i++) 
     nameArray[i] = new string[2]; //evvery index have 2 column one for first name and second for last name 
    //2D array done 
    in.open("File.txt"); 
    for (int i = 0; i < count; i++) 
    { 
     for (int j = 0; j < 2; j++) 
     { 
        // getline(in,nameArray[i][j]; // if you want to read sentence.not a single word. 
      //in >> nameArray[i][j]; //if you want to read name or a single word. 
     } 
    } 
    in.close(); 
    for (int i = 0; i < count; i++) 
    { 
     for (int j = 0; j < 2; j++) 
     { 
      cout<<nameArray[i][j]<<" "; //Printing [i,j] with first and second name 
     } 
     cout << endl; 
    } 
    for (int i = 0; i < count; i++) 
     delete[] nameArray[i]; 
    delete[] nameArray; 
    system("pause"); 
    return 0; 
} 

輸出

James Will 
Bruce Wayne 
Harry Potter 
相關問題