简体   繁体   中英

C++ Index Array Print & Deleting a string name Array Print

I've had a difficult time trying to figure out a problem my professor has assigned. I believe I've figured out the second part of the 3 items listed below, but cannot figure out at all how to do the 1st or 3rd part. Please help! Thank you very much in advance.


Write one program.

Create a string array in the main(), called firstNameArray, initialize with 7 first names

Jim, Tuyet, Ann, Roberto, Crystal, Valla, Mathilda

Write a first function that passes in a single name and the array into a function and have it search the array to see if the name in the firstNameArray. If found, it will return the array index where the name is found, else it return the number 7. (Other parameters may be required to be passed into the function)

Write the code in the main program to call the function. Check the return value, and either prints the name using the index number, or prints 'name not found'.

Write a second function that will print all the names in the array. Pass the array into the function. (Several parameters are required to be passed into the function)

Write the code in the main program to call this function.

Write a third function that will delete a name from the array. Check first to see if the name is in the array, before you try to delete it (use the first function). You are not reducing the size of the array… Just making one spot blank. (Other parameters may be required to be passed into the function). Call the Print the Whole array after you call this function.

Print out the array… it the spot is blank do not print it.


I've gotten this so far for the second part:


#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <string>
using namespace std;


void printArray(string array[], int size);

int main()
{
string firstNameArray[7];

firstNameArray[0] = "Jim";
firstNameArray[1] = "Tuyet";
firstNameArray[2] = "Ann";
firstNameArray[3] = "Roberto";
firstNameArray[4] = "Crystal";
firstNameArray[5] = "Valla";
firstNameArray[6] = "Mathilda";

printArray(firstNameArray, 7);

system("pause");
return 0;
}

void printArray(string array[], int size){
for (int i = 0; i < size; i++){
    cout << array[i] << endl;
}
}

Try This.

#include "stdafx.h"

#include <string>
#include <iostream>

int FindName(std::string* names, std::string name, int size) {
    for (int i = 0; i < size; i++) {
        if(names[i] == name)
            return i;
    }

    return size;
}

void PrintNames(std::string* names, int size) {
    for (int i = 0; i < size; i++) {
        std::cout << names[i] << std::endl;
    }
}


void DeleteName(std::string* names, std::string name, int size) {
    if(FindName(names, name, size) == size)
        return;

    for (int i = 0; i < size; i++) {
        if(names[i] == name)
            names[i] = "\0";
    }
}


int main()
{
    std::string firstNameArray[7];

    firstNameArray[0] = "Jim";
    firstNameArray[1] = "Tuyet";
    firstNameArray[2] = "Ann";
    firstNameArray[3] = "Roberto";
    firstNameArray[4] = "Crystal";
    firstNameArray[5] = "Valla";
    firstNameArray[6] = "Mathilda";

    int index = FindName(firstNameArray, "Ann", 7);

    if(index == 7)
        std::cout << "Name not found" << std::endl;
    else
        std::cout << std::to_string(index) << std::endl;

    PrintNames(firstNameArray, 7);
    DeleteName(firstNameArray, "Ann", 7);

    std::cout << "\n";

    PrintNames(firstNameArray, 7);

    ::getchar();
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM