2015-12-01 79 views
-3

我有一個指向我創建的變量「carrierTime」的對象錯誤類型的指針。如果我使這個數組,carrierTime成爲第一個if語句中的錯誤,但是如果我把它留在沒有任何數組中,我會在代碼的最後一行發生錯誤,我在乘法中使用了carrierTime。 任何人都可以幫忙嗎?必須有指向對象類型C++(數組)的指針

使用平臺:視覺工作室

#include "AMcore.h" 
#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string> 

using namespace std; 

int main() 
{ 
cout << "Amplitude Modulation Coursework" << endl; 
cout << "Name: Mohammad Faizan Shah" << endl; 
cout << "Student ID: 5526734 \n\n\n" << endl; 

std::ifstream file,file2; 
string filename1,filename2; 
int rowCounter = 0; 
double informationTime; 
double informationAmplitudeAmount[361]; 
long double carrierTime; 
double carrierAmplitudeAmount[361]; 
double totalAmplitudeAmount[1000]; 

int plotPoint; 





cout << "Please enter the filename of the Carrier wave \n" << endl; 
cin >> filename1; 



file.open("carrier.txt"); 

if (file.is_open()) 
{ 



    file >> carrierTime; 

    while (!file.fail()) 
    { 
     cout << "row" << setw(3) << rowCounter; 

     cout << " Time = " << setw(5) << carrierTime; 


     file >> carrierAmplitudeAmount[rowCounter]; 
     rowCounter++; 

     if (!file.fail()) 
     { 
      cout << " Carrier signal= " << setw(5) << carrierAmplitudeAmount; 
      file >> carrierTime; 
     } 
     cout << endl; 
    } 

    if (file.eof()) 
     cout << "Reached the end of file marker" << endl; 
    else 
     cout << "Error whilst reading input file" << endl; 
} 
else 
{ 
    cout << "Error opening input file, "; 
    cout << "check carrier.txt exists in the current directory." << endl; 
} 

file.close(); 


cout << "\n\n" << endl; 
cout << "Please enter the filename of the information wave \n\n\n" << endl; 
cin >> filename2; 



file2.open("information.txt"); 

if (file2.is_open()) 
{ 



    file2 >> informationTime; 

    while (!file2.fail()) 
    { 
     cout << "row" << setw(3) << rowCounter; 

     cout << " Time = " << setw(5) << informationTime; 


     file2 >> informationAmplitudeAmount[361]; 
     rowCounter++; 

     if (!file2.fail()) 
     { 
      cout << " Carrier signal= " << setw(5) << informationAmplitudeAmount; 
      file2 >> informationTime; 
     } 
     cout << endl; 
    } 

    if (file2.eof()) 
     cout << "Reached the end of file marker" << endl; 
    else 
     cout << "Error whilst reading input file" << endl; 
} 
else 
{ 
    cout << "Error opening input file, "; 
    cout << "check carrier.txt exists in the current directory." << endl; 
} 

file.close(); 

cout << "Reading from txt file has completed" << endl << endl; 


cout << "\n\n" << endl; 
cout << "\n\n" << endl; 



cout << "please enter number of sample points to plot:| \n" << endl; 
do{ 
    cin >> plotPoint; 

    if (plotPoint <= 361) 
    { 
     cout << "\n plotting the graph.\n" << endl; 
    } 
    else if (plotPoint > 361) 
    { 
     cout << "Value is too high.. Try value lower than 361\n" << endl; 

    } 
} while (plotPoint > 361); 

cout << "row" << setw(3) << rowCounter; 
file >> carrierAmplitudeAmount[361]; 
rowCounter++; 

plotPoint = 361/plotPoint; 

cout << " Time \|   Amplitude Modulation plot\n------------+--------------------------------------------------\n"; 
totalAmplitudeAmount[0] = carrierAmplitudeAmount[0] * informationAmplitudeAmount[0]; 
cout << setw(6) << carrierTime << setw(4) << "\|" << setw(48) << "*" << totalAmplitudeAmount[0] << endl; 


for (int i = 1; i <= 361; i = i + plotPoint) { 
    totalAmplitudeAmount[i] = informationAmplitudeAmount[i] * carrierAmplitudeAmount[i]; 

    int y = totalAmplitudeAmount[i] * 22; 



    cout << setw(6) << carrierTime[i++] << setw(4) << "\|" << setw(26 + y) << "*" << totalAmplitudeAmount[i] << endl; 


} 












cout << "End of program" << endl; 


system("pause"); 

return 0; 
} 

回答

1
cout << setw(6) << carrierTime[i++] << setw(4) << "\|" << setw(26 + y) << "*" << totalAmplitudeAmount[i] << endl; 

carrierTime[i++]看起來並不正確。該變量未被定義爲指針。

此外,正確的調試將幫助您爲自己捕獲這些錯誤。

+0

我得到正確的輸出carrierTime應該是carrierTime [i] ..但是我得到的指針問題 –

+0

我不明白你的意見。 – Xirema

+0

你說carrierTime與[i ++]看起來不正確,但是爲了得到正確的輸出載波時間應該有[i]。它仍然給我一個指針問題。 –

相關問題