Vinnaren i pepparkakshustävlingen!
2022-10-02, 10:35
  #1
Medlem
evil.drunk.clowns avatar
Hur f*n anropar man funktionen korrekt? I filen vector_strings.cpp har jag skrivit "Adder();" och där behövs tydligen någon parameter som ska in i "Adder(std::vector<std::string>& names)". Finns här någon kunnig själ som kan hjälpa till lite?

Kod:
******************
main.cpp
******************
#include <iostream>

#include "vector_strings.hpp"

int main(void) {
    CMDReader();
    return 0;
}


******************
vector_strings.hpp
******************
#ifndef VECTOR_STRINGS
#define VECTOR_STRINGS

#include <vector>
#include <string>

void CMDReader();
void Adder(std::vector<std::string>& names);
void Remover(std::vector<std::string>& names);
void Printer(std::vector<std::string>& names);

#endif


******************
vector_strings.cpp
******************
/*
Implement a program that stores and removes given strings from a
vector, as commanded by the user. You should implement a simple
command line interface that implements the following.

In the beginning, program should print `Commands: ADD, PRINT, REMOVE, QUIT`, 
followed by a newline. Then print `Enter a command:` followed by a newline. 
Then the program should read the command from the user and do the following:

  * **ADD**: Add a given string to the vector. The program should first
    prompt: `Enter a name:` (without trailing space) followed by a newline. Then
    it reads the name from the user and adds it to a the
    vector. Finally, it prints: `Number of names in the vector:`, a
    newline, the size of the vector, and finally newline. The ADD
    functionality is implemented in the function `Adder`.
  
  * **REMOVE**: Removes the last string from the vector. This
    operation is implemented in the function `Remover`. The function
    should print the removed string in the following way: `Removing
    the last element:`, followed by a newline, then the string and a
    newline.

  * **PRINT**: Outputs all stored strings, each on a separate line
    (e.g., followed by newline character). This operation is
    implemented in function `Printer`.

  * **QUIT**: Exit the program.

In addition to the above three functions, you need to implement
the function `CMDReader` that parses the commands and calls the
appropriate functions. The detailed function interfaces can be found
in the file *vector_strings.hpp*.

Here is an example of an session:

Commands: ADD, PRINT, REMOVE, QUIT
Enter a command:
ADD
Enter a name:
Kalle
Number of names in the vector:
1
Enter a command:
ADD
Enter a name:
Nisse
Number of names in the vector:
2
Enter a command:
PRINT
Kalle
Nisse
Enter a command:
REMOVE
Removing the last element:
Nisse
Enter a command:
QUIT
*/
#include "vector_strings.hpp"

#include <iostream>
#include <vector>
#include <string>

void Adder(std::vector<std::string>& names) {

  std :: cout << "Enter a name:\n";
  
  }

void Remover(std::vector<std::string>& names) {
    
}

void Printer(std::vector<std::string>& names) {
    
}

void CMDReader() {      

      while (true)
      
      {

        std :: cout << "Commands: ADD, PRINT, REMOVE,QUIT\n";

        std :: cout << "Enter a command:\n";

        std :: string command;

        std :: cin >> command;

        if (command == "ADD")
    
        {

          Adder();
        
        }

        if (command == "PRINT")
    
        {

           Printer();
        
        }

        if (command == "REMOVE")
    
        {

          Remover();
        
        }

        if (command == "QUIT")
    
        {

        break;
        
        }

        
      
      }

}
Citera
2022-10-02, 10:53
  #2
Medlem
Enterprises avatar
Citat:
Ursprungligen postat av evil.drunk.clown
Hur f*n anropar man funktionen korrekt? I filen vector_strings.cpp har jag skrivit "Adder();" och där behövs tydligen någon parameter som ska in i "Adder(std::vector<std::string>& names)". Finns här någon kunnig själ som kan hjälpa till lite?

Kod:
******************
main.cpp
******************
#include <iostream>

#include "vector_strings.hpp"

int main(void) {
    CMDReader();
    return 0;
}


******************
vector_strings.hpp
******************
#ifndef VECTOR_STRINGS
#define VECTOR_STRINGS

#include <vector>
#include <string>

void CMDReader();
void Adder(std::vector<std::string>& names);
void Remover(std::vector<std::string>& names);
void Printer(std::vector<std::string>& names);

#endif


******************
vector_strings.cpp
******************
/*
Implement a program that stores and removes given strings from a
vector, as commanded by the user. You should implement a simple
command line interface that implements the following.

In the beginning, program should print `Commands: ADD, PRINT, REMOVE, QUIT`, 
followed by a newline. Then print `Enter a command:` followed by a newline. 
Then the program should read the command from the user and do the following:

  * **ADD**: Add a given string to the vector. The program should first
    prompt: `Enter a name:` (without trailing space) followed by a newline. Then
    it reads the name from the user and adds it to a the
    vector. Finally, it prints: `Number of names in the vector:`, a
    newline, the size of the vector, and finally newline. The ADD
    functionality is implemented in the function `Adder`.
  
  * **REMOVE**: Removes the last string from the vector. This
    operation is implemented in the function `Remover`. The function
    should print the removed string in the following way: `Removing
    the last element:`, followed by a newline, then the string and a
    newline.

  * **PRINT**: Outputs all stored strings, each on a separate line
    (e.g., followed by newline character). This operation is
    implemented in function `Printer`.

  * **QUIT**: Exit the program.

In addition to the above three functions, you need to implement
the function `CMDReader` that parses the commands and calls the
appropriate functions. The detailed function interfaces can be found
in the file *vector_strings.hpp*.

Here is an example of an session:

Commands: ADD, PRINT, REMOVE, QUIT
Enter a command:
ADD
Enter a name:
Kalle
Number of names in the vector:
1
Enter a command:
ADD
Enter a name:
Nisse
Number of names in the vector:
2
Enter a command:
PRINT
Kalle
Nisse
Enter a command:
REMOVE
Removing the last element:
Nisse
Enter a command:
QUIT
*/
#include "vector_strings.hpp"

#include <iostream>
#include <vector>
#include <string>

void Adder(std::vector<std::string>& names) {

  std :: cout << "Enter a name:\n";
  
  }

void Remover(std::vector<std::string>& names) {
    
}

void Printer(std::vector<std::string>& names) {
    
}

void CMDReader() {      

      while (true)
      
      {

        std :: cout << "Commands: ADD, PRINT, REMOVE,QUIT\n";

        std :: cout << "Enter a command:\n";

        std :: string command;

        std :: cin >> command;

        if (command == "ADD")
    
        {

          Adder();
        
        }

        if (command == "PRINT")
    
        {

           Printer();
        
        }

        if (command == "REMOVE")
    
        {

          Remover();
        
        }

        if (command == "QUIT")
    
        {

        break;
        
        }

        
      
      }

}
Vad är det du ska åstadkomma med övningen?
Jo, ett program där användare kan lägga till namn (strängar) i en vector (form av lista).
Funktionen Adder har ju ett argument nämligen av datatypen std::vector<std::string> (låt vara att funktionen tar in pekaren genom &-nyckelordet).
Då måste ju din huvudfunktion definera och initiera en sådan variabel som sedan ges som ett argument till Adder.

https://www.programiz.com/cpp-programming/vectors

En vector är en dynamisk lista. Jag kör själv sällan c++ men kunde googla en lösning på en minut, så läs på lite, snälla.
Citera

Stöd Flashback

Flashback finansieras genom donationer från våra medlemmar och besökare. Det är med hjälp av dig vi kan fortsätta erbjuda en fri samhällsdebatt. Tack för ditt stöd!

Stöd Flashback