【解決方法】C ++すべてのリンクされたリストデータをリストする方法と、forループを使用して出力座標位置用のIをインクリメントする方法


#include <iostream>
#include <string.h>
#include <string>
#include <iomanip>
#include <cctype>
#include <ctype.h>
#include <fstream>
#include <cstdlib>
#include <windows.h>
using namespace std;

struct student{
	string studentid,yearlevel,fullname,birthday,address,gender,course;
	student *next;
};

void mainmenu(){
cout << "					[I] Student Information System [I]"   << endl;
cout << "					 |   What do you want to do?    |"    << endl;
cout << "					 |                              |"    << endl;
cout << "					 |   1. Add New Record          |"    << endl;
cout << "					 |   2. Search Record           |"    << endl;
cout << "					 |   3. Display All Records     |"    << endl;
cout << "					 |   4. Display Specific Record |"    << endl;
cout << "					 |   5. Delete Record           |"    << endl;
cout << "					 |   6. Exit                    |"    << endl;
cout << "					 ********************************"    << endl;

cout << "\t\t\t\t\t    Please type your selection:";
}

void border(){
cout <<"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////"<< endl;
}

void gotoxy(short x,short y){
COORD pos={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}

void printrecords(student *head){
bool x;	

cout <<"\n\n";
//cout <<"Student-ID  Fullname			Gender  Yearlevel  Course    Birthday  Address            \n";

system("CLS");
gotoxy(0,1);
cout<<"Student ID";
gotoxy(12,1);
cout<<"Full Name";
gotoxy(30,1);
cout<<"Gender";

gotoxy(39,1);
cout<<"Year Level";
gotoxy(51,1);
cout<<"Course";
gotoxy(61,1);
cout<<"Birthday";
gotoxy(71,1);
cout<<"Address\n\n";

while(head!=NULL){
for(int i=2;x != false;){
	gotoxy(0,i);
	cout<<head->studentid;
	gotoxy(12,i);
	cout<<head->fullname;
	gotoxy(30,i);
	cout<<head->gender;
	gotoxy(39,i);
	cout<<head->yearlevel;
	gotoxy(51,i);
	cout<<head->course;
	gotoxy(61,i);
	cout<<head->birthday;
	gotoxy(71,i);
	cout<<head->address;
	
	i++;
	
	head = head->next;
	
	if (head->NULL){
		x=false;
	}
}

x = true;

cout <<"\n\n";
}


}

void addrecord(student **head){
//NEW NODE

student *newnode = new student;

cout <<"Enter Student ID: ";
	cin >> newnode->studentid;
cout <<"\nEnter Fullname: ";
	cin.ignore();
	getline(cin,newnode->fullname);
cout <<"\nEnter Gender (M/F): ";
	getline(cin,newnode->gender);
cout <<"\nEnter Yearlevel(1/2/3/4/5): ";
	cin >> newnode->yearlevel;
cout <<"\nEnter Course (BS______): ";
	cin.ignore();
	getline(cin,newnode->course);
cout <<"\nEnter Birthday (MM/DD/YYYY): ";
	getline(cin,newnode->birthday);
cout <<"\nEnter Address: ";
	getline(cin,newnode->address);

newnode->next = NULL;

// CHECK IF LIST IS EMPTY
if(*head==NULL)
{
*head = newnode;
return;
}

//TRANSVERSING LIST
student *temp = *head;
while(temp->next!=NULL)
{
temp = temp->next;
}
//add the newnode at the end of the list
temp->next = newnode;
}

void searchrecord(string searchValue,student *head){
	//TEMP NODE POINT TO HEAD
	student* temp=head;
	
	//DECLARE 2 VAR to: TRACK | SEARCH
	int found = 0;
	int i=0;
	
	/*CHECK temp node if NULL else check node DATA with searchValue, 
	if found update and break; 
	else continue searching till temp node is not null */
	if(temp != NULL) {
    while(temp != NULL) {
      i++;
      if(temp->studentid == searchValue) {
        found++;
        break;
      }
      temp = temp->next;
    }
    if (found == 1) {
      cout<<"\n "<<searchValue<<" Details:\n";
      cout<<searchValue<<" is numbered "<<i<<"on the database.\n";
      
      cout <<"Student-ID  Fullname			Gender  Yearlevel  Course    Birthday  Address            \n";
		while(temp!=NULL){
		cout<<left<<setw(9)<<temp->studentid;
		cout<<left<<setw(10)<<temp->fullname;
		cout<<left<<setw(10)<<temp->gender;
		cout<<left<<setw(10)<<temp->yearlevel;
		cout<<left<<setw(10)<<temp->course;
		cout<<left<<setw(10)<<temp->birthday;
		cout<<left<<setw(10)<<temp->address<<endl;
		temp = NULL;
		cout<<"\n\n";
		}
    }
	 else {
      cout<<searchValue<<" is not in the database.\n";
    }
  } else {
    cout<<"Their is no data in the database yet...\n";
  }
}

int main(){
	int select;
	string u,searchValue;
	bool system=false;

	student *head=NULL;


do{
	
	border();
	mainmenu();
	
	cin >> select;

	
	switch (select){
	case 1: { //Add New Record 
		cout<<"Adding New Record\n\n";
		addrecord(&head);
		break;
	}
	
	case 2: { //Search Record 
		cout<<"Accessesing Database Records....\n";
		cout<<"Input the Student-ID that you would want to search:\n";
		cout<<"Student-ID: ";
			cin>>searchValue;
		searchrecord(searchValue,head);
		break;
	}
	
	case 3: { //Display All Records 
		cout<<"Displaying All Records.....\n\n";
		cout<<"Database Record(s)\n";
		printrecords(head);
		break;
	}
	
	case 4: { //Display Specific Record
		cout<<"Ran case 4\n";
		break;
	}
	
	case 5: { //Delete Record
		cout<<"Ran case 5\n";
		break;
	}
	
	case 6:{ //EXIT
		cout<<"Ran case 6\n";
		cout<<"Exiting Program Have a Good Day!";
		system = true;
		break;
	}
	default:{
		cout <<"\nInvalid Input... \n Try Again...\n";
		break;
	}
	} //end switch
}while(!system);

return 0;
}	// end main

In void print records 方法

私が試したこと:

forループで他の条件を作ってみました

for(int i=2;x != false;i++){ ... x=false;}

しかし、それはループするか、最新の最初の入力のみを記録しません

新しい試行を編集
1,1,1,1,1、次に2,2,2,2,2を入力してこのコードを試しました
レコードを検索すると両方を見つけることができますが、表示すると空白になり、printrecord のコードに問題があり、混乱しています….

while(head!=NULL){
for(int i=2;x != false;){
	gotoxy(0,i);
	cout<<head->studentid;
	gotoxy(12,i);
	cout<<head->fullname;
	gotoxy(30,i);
	cout<<head->gender;
	gotoxy(39,i);
	cout<<head->yearlevel;
	gotoxy(51,i);
	cout<<head->course;
	gotoxy(61,i);
	cout<<head->birthday;
	gotoxy(71,i);
	cout<<head->address;
	
	i++;
	
	head = head->next;
	
	if (head->next = NULL){
		head = NULL;
		x=false;
	}
}

cout <<"\n\n";
}

解決策 1

要素の数がわからない場合 (そのコードからは明らかにわかりません)、最も簡単な解決策はダンプすることです x コレクションの最後に到達したら、 break ループを終了するには:

C++
if (head->next = NULL){
    head = NULL;
    break;
}

しかし、私はおそらく while 代わりにループします。

見積もり:

for ループは、表形式を形成するために出力のインクリメントを下げるためのものです。
上記のコードからわかるように、タイトルの y の座標が 3 であるため、i は 5 から始まります。

わかりました、5 は正当化されますが、なぜ for ループなのでしょうか? 特に for ループとして使用しないため、本体の後にインクリメントはありません。

このコードでは得られないことで、何が得られるのでしょうか。

int lineNo = 5;
while (head != null)
   {
   ...
   lineNo++;
   head = head->next;
   }

はい、代わりにここで for ループを使用できますが、読みにくくなります。

for (int lineNo = 5; head != null; lineNo++, head = head-.next)
   {
   ...
   }

また、読みにくい場合は、間違いが発生しやすくなります。
あなたのバージョンは 2 つのループを入れ子にしようとして、本当にばかげたものを生成します!
それは今意味がありますか?

コードに飛び込む前に、常に座って考えてください。頭を悩ませる時間を節約できます。

解決策 3

これが私のコードである場合、次のような出力関数を記述します。

C++
template< typename T >
void outputxy( std::ostream & os, int x, int y, const T & v )
{
	gotoxy( x, y );
	os << v;
}

次に、次のようなコード:

C++
gotoxy(0,i);
cout<<head->studentid;
gotoxy(12,i);
cout<<head->fullname;
gotoxy(30,i);
cout<<head->gender;

次のようになります。

C++
outputxy(  0, i, head->studentid );
outputxy( 12, i, head->fullname );
outputxy( 30, i, head->gender );

この種のことは、コードをかなり単純化できます。

解決策 2

void printrecords(student *head){
//cout <<"Student-ID  Fullname			Gender  Yearlevel  Course    Birthday  Address            \n";
system("CLS");

cout<<"Displaying All Records.....\n";
cout<<"Database Record(s)\n";

gotoxy(0,3);
cout<<"Student ID";
gotoxy(12,3);
cout<<"Full Name";
gotoxy(30,3);
cout<<"Gender";
gotoxy(39,3);
cout<<"Year Level";
gotoxy(51,3);
cout<<"Course";
gotoxy(61,3);
cout<<"Birthday";
gotoxy(71,3);
cout<<"Address\n\n";

while(head!=NULL){
	for(int i=5;i<=100 && head!= NULL;i++){
	gotoxy(0,i);
	cout<<head->studentid;
	gotoxy(12,i);
	cout<<head->fullname;
	gotoxy(30,i);
	cout<<head->gender;
	gotoxy(39,i);
	cout<<head->yearlevel;
	gotoxy(51,i);
	cout<<head->course;
	gotoxy(61,i);
	cout<<head->birthday;
	gotoxy(71,i);
	cout<<head->address;
	
	head = head->next;
	}
cout <<"\n\n";
}
}

コメント

タイトルとURLをコピーしました