2015-12-01 73 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]。它仍然给我一个指针问题。 –

相关问题