以下有個地方錯誤
也就是 ch[0] = b2[atol(temp)];
您可以改改看
(程式寫的不太好,因為個人都是玩 VB,c++很久才碰一次 ^^||)
#include <iostream>
#include <fstream>
using namespace std;
ifstream infile; //宣告一個file物件
void read_line(char string[]) {
int i=0;
do {
string[i] = (char)infile.get();
i++;
} while ((string[i-1] != '\n') && infile.good());
string[i-1] = '\0';
}
//接著是 main主體
int main () {
char b1[255];//*b1; //宣告b1字串
char b2[255];//*b2; //宣告b2字串
char ch[]={'0','\0'}, temp[255];
int i;
//開啟 test.txt 檔案 : void open ( const char * filename, openmode mode = in );
infile.open("test.txt");
//讀第一行到字串b1中
read_line(b1);
//讀第二行到字串b2中
read_line(b2);
//輸入取第幾個字元當乘數
printf("請從[ %s ]選第n個數當乘數:",b2);
scanf("%s", temp);
ch[0] = b2[atol(temp)];
//輸出結果
printf("%s * %l = %l \n", b1, atol(ch), atol(b1)*atol(ch));
infile.close(); //關閉檔案
system("PAUSE");
return 0; //main 是一個函式要傳出一個值
}
|