C++突然出现iostream问题

C++ Sudden iostream issues

本文关键字:iostream 问题 突然 C++      更新时间:2023-10-16

我正要为我的项目发送我完成的代码,却突然遇到了一连串模棱两可的"cout"answers"cin"错误,以及一些需要声明的if和while块。起初我以为我缺少iostream或命名空间,但这似乎不是问题所在。有线索吗?

#include <iostream>
#include <Windows.h>
#include <string>
#include "ARRAY.H"
using namespace std;
void addToArray(Array<int> &intArray);
void linearSearch();
void binarySearch(Array<int> &intarray, int beg, int end);
void printArray();
void sortArray();
void Remove(int p_index);
void clear();
void testFunctions();
void pop();
    Array<int> intarray(10);
    int num_elements = 10;

int main()
{
    intarray[0] = 42;
    intarray[1] = 6;
    intarray[2] = 2;
    intarray[3] = 51;
    intarray[4] = 16;
    intarray[5] = 13;
    intarray[6] = 44;
    intarray[7] = 19;
    intarray[8] = 26;
    intarray[9] = 88;
    intarray.WriteFile("unorderedArray.txt");
    testFunctions();
}
// -------------------------------------------------------
//  Name:         testFunctions
//  Description:  General testing of all the functions
//  Arguments:    None.
// -------------------------------------------------------
void testFunctions()
{
    intarray.ReadFile("unorderedArray.txt");
    printArray();
    sortArray();
    Remove(4);
    addToArray(intarray);
    linearSearch();
    binarySearch(intarray, 0, intarray.Size());

    //clear();
    pop();

    printArray();

    intarray.WriteFile("orderedArray.txt");
    cout<<"nThis concludes our tests. Full marks please!"<<endl;
    Sleep(10000);

}
// -------------------------------------------------------
//  Name:         addToArray
//  Description:  Allows users to insert a new element into the array
//                at an index of their choosing
//  Arguments:    The Array
// -------------------------------------------------------
void addToArray(Array<int> &intarray)
{
    int newValue; //the value to be added to the array
    int newIndex; //the index location of the new array
    int grow_size; //the amount the array will grow by if it is full
    if(num_elements == intarray.Size() ) //If array is full but user wants to add to it, they will be prompted to increase it's size before being allowed to add an elememt
        {
            cout<<"nArray is full!!!!! How much do you want to grow it?"<<endl;
            cin>> grow_size;
            intarray.Resize(intarray.Size() + grow_size); //creates a new larger array and copies everything over
            cout<<"Array resized"<<endl;
        }
    cout<<"What do you want to add to the array?"<<endl;
    cin >> newValue;
    cout<<"At what point should this value be added?"<<endl;
    cin >> newIndex;
    intarray.Insert(newValue, newIndex);
    num_elements++; 
    sortArray(); //sorts the values from smallest to largest
    printArray();
}
// -------------------------------------------------------
//  Name:         linearSearch
//  Description:  Searches through the Array from start to finish for the
//                inputted value.
//  Arguments:    None.
// -------------------------------------------------------
void linearSearch()
{
    int search; //the search key
    int result = 0; 
    cout<<"nEnter Element you want to Search=";
    cin>>search;
    for(int i=1;i<=intarray.Size();i++)  //goes through each element of the array until it finds the value in the key.                    
        if(intarray[i]==search)
            {
            cout<<"nData is Found at Location : "<<i;
            result=1;
            break;
            }
        }
        if(result==0)
            {
            cout<<"Data is Not Found";
            }
        cout<<"n"<<endl;
}
// -------------------------------------------------------
//  Name:         binarySearch
//  Description:  Uses a B-Tree format to search for inputted 
//                values of a sorted array
//  Arguments:    The Array, int beg: the start of the Array, int end: The end of the Array
// -------------------------------------------------------
void binarySearch(Array<int> &intarray, int beg, int end)
{
        int key; //the value to be searched for
        cout<<"nEnter Item you want to Search= ";
        cin>>key;
        int mid; // the middle of the array
        //beg=1;
        //end=intarray.Size();
        mid=(beg+end)/2; //the middle point of the array is found by adding the beginning and end values and dividing them by two.                      
        while(beg<=end && intarray[mid]!=key) //While the mid value does not equal the key, if it is smaller it adds 1 to the beginning but if it is            
            //larger it subtracts 1 from the end and then the value of the middle point is found again. This continues until either the mid value equals the key value  
        {
        if(intarray[mid]<key)
        beg=mid+1;
        else
        end=mid-1;
        mid=(beg+end)/2;
        }
        if(intarray[mid]==key)
        {
        cout<<"nData is Found at Location : "<<mid;
        }
        else
        {
        cout<<"Data is Not Found";
        }
} 
// -------------------------------------------------------
//  Name:         printArray
//  Description:  prints the array
//  Arguments:    None.
// -------------------------------------------------------
void printArray()
{
    int i = intarray[0];

    cout<<"n The contents of the array are : "<<endl;
       cout<<"n     Elements :"<<"tt     Value:"<<endl;
       for (i=0;i<intarray.Size();i++) //goes through each element of the array and prints them nice and neat
         cout<<" tarray ["<<i<<"]"<<"tt  "<<intarray[i]<<endl;
}
// -------------------------------------------------------
//  Name:         sortArray
//  Description:  sorts the Array from smallest value to largest
//  Arguments:    None.
// -------------------------------------------------------
void sortArray()
{
    cout<<"nSorting array..."<<endl;
        for (int i = 0; i < intarray.Size(); i++)
    {
        // nSmallestIndex is the index of the smallest element found so far
        int nSmallestIndex = i;
        // Searches through every element starting at nStartIndex+1
        for (int nCurrentIndex = i + 1; nCurrentIndex < intarray.Size(); nCurrentIndex++)
        {
            // If the current element is smaller than the previously found smallest it becomes the new smallest index
            if (intarray[nCurrentIndex] < intarray[nSmallestIndex])
                nSmallestIndex = nCurrentIndex;
        }
        // Swap the start element with the smallest element
        swap(intarray[i], intarray[nSmallestIndex]);

    }
        printArray();
}
// -------------------------------------------------------
//  Name:         Remove
//  Description:  Removes an element from the array and
//                resizes it.
//  Arguments:    int p_index: The index of the element to be deleted
// -------------------------------------------------------
void Remove(int p_index)
{
int index;
    cout<<"nRemoving index "<<p_index<<" from array"<<endl;
          // move everything after the deleted element down by one cell.
          for(index = p_index + 1; index < intarray.Size(); index++)
          {
              intarray.m_array[index - 1] = intarray.m_array[index];
          }
          //Resizes the array by reducing it by one so that the last value is not duplicated
          intarray.Resize(intarray.Size() -1);
          num_elements--;
          printArray();
 }
// -------------------------------------------------------
//  Name:         clear
//  Description:  "clears" the array by changing all the values to 0
//  Arguments:    None.
// -------------------------------------------------------
void clear()
{
    //goes through each element of the array and changes all the values to 0.
    for(int i=0; i < intarray.Size(); i++)
        {
        intarray[i]=0;
        }
    printArray();
}
// -------------------------------------------------------
//  Name:         pop
//  Description:  Pops off the last element of the array
//  Arguments:    None.
// -------------------------------------------------------
void pop()
{
    //Reduces the size of the array by one, chopping off the last element.
    cout<<"nPopping off the last element of the Array"<<endl;
          intarray.Resize(intarray.Size() -1);
          num_elements++;
}

错误:

1> c:\users\liam\desktop\orderedarray\orderedarray\main.cpp(198):错误C2872:"cout":不明确的符号1>可以是"c:\users\利亚m\desktop \orderedrray\orderedarry\main.cp(144):int cout'1>或"c:\program files(x86)\microsoft visual studio 10.0\vc\include\iostream(26):std::ostream std::cout'1>c:\users\ liam\ddesktop\orderedarray\main.cpp(198):错误C2297:'<lt;':非法,右操作数的类型为"const char[35]"1>c:\users\liam\datadesk\orderedarray\orderedrray\main.cpp(198):错误C2563:形式参数lis 不匹配

等一下,我自己解决了。我浏览了错误消息,发现其中一个函数中缺少一个括号。代码现在似乎运行得很好,所以我想所有新手程序员的道德准则是,如果你的整个程序突然充满了错误,请检查所有括号是否正确配对。