Question

In: Computer Science

Write a program in C++ that will make changes in the list of strings by modifying...

Write a program in C++ that will make changes in the list of strings by modifying its last element.
Your program should have two functions:
1. To change the last element in the list in place. That means, without taking the last element from
the list and inserting a new element with the new value.
2. To compare, you need also to write a second function that will change the last element in the list
by removing it first, and inserting a new element with the new value.

The main creates the list, creates the string variables, populates few elements in the list (couple is
enough) and calls the functions to modify the list. After each call to the functions, the main should print
out the value of the last element in the list, so the changes are visible. Again, you print the last element
not from within the functions, but from the main.
Note 1: You will need to use references to implement the in-place requirement.
Note 2: However difficult that assignment may look like, all information you need on how to work with
the list is given to you in the lecture and both your functions implementation are just very few short
statements (hint: If you find yourself writing 5 or more lines of codes for each function, you probably
do it wrong).

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

What i have so far, but keeps on getting an error once the functions are called.

// StringsList.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include
#include
#include
#include
#include

using namespace std;

void funcWithList(list &, const std::string &);
void replacementForList(list &, const std::string &);

int main()
{
   ofstream out_data("Output.txt", ios::out);
   //end of copy code
   if (!out_data)
   {
       cout << "Error in opening the file, Output.txt";
       getchar();
       exit(1);

   }

std::cout << "Hello World!\n";

   list aList; //create list taking std::string
std::string input;

//get input to change last string

std::cin >> input;
   out_data << input;
   std::string s = input;

   //I can pass list to the function like that:
   funcWithList(aList, s);
   replacementForList(aList, s);

   getchar();
   out_data.close();//close output.txt file
   return 0;
}

void funcWithList(list &, const std::string &)
{
   ofstream out_data("Output.txt", ios::out);
   //end of copy code
   if (!out_data)
   {
       cout << "Error in opening the file, Output.txt";
       getchar();
       exit(1);

   }

   list tempList //myList.push_front;


   //I can access the last element like that:
   std::cout << "\nThe last element is: " << & tempList.back();

tempList.pop_back();  

std::string input;

//get input to change last string

std::cin >> input;
   out_data << input;
   std::string s = input;


   tempList.push_back(s);
}

void replacementForList(list &, const std::string &)
{
   ofstream out_data("Output.txt", ios::out);
   //end of copy code
   if (!out_data)
   {
       cout << "Error in opening the file, Output.txt";
       getchar();
       exit(1);

   }

   list tempList //myList.push_front;


   //I can access the last element like that:
   std::cout << "\nThe last element is: " << &tempList.back();

tempList.pop_back();  

std::string input;

//get input to change last string

std::cin >> input;
   out_data << input;
   std::string s = input;


   tempList.push_back(s);
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

no examples or expected outputs were given but did state: "The main creates the list, creates the string variables, populates few elements in the list (a couple is
enough) and calls the functions to modify the list. After each call to the functions, the main should print
out the value of the last element in the list, so the changes are visible. Again, you print the last element
not from within the functions, but from the main."

therefore would look something like...

Fill your list with names...

Freddie

Nick

James

Rick

Now function calls

modify using references

what new name to use?

Robert

print from main, the last element

Robert

modify using deletion

what new name to use?

Tim

print from main, the last element

Tim

Solutions

Expert Solution

Code:

#include <list>
#include <string>
#include <iostream>

using namespace std;

void funcWithList(list<string> &, const std::string &);
void replacementForList(list<string> &, const std::string &);

int main()
{

   list<string> aList; //create list taking std::string

   aList.push_back("Freddie");
   aList.push_back("Nick");
   aList.push_back("James");
   aList.push_back("Rick");

   cout << "\nNames in the list\n";

   list<string>::iterator it = aList.begin();

   while (it != aList.end())
   {
       cout << "\n" << *it;
       it++;
   }

   cout << "\nNow function calls\n";
  
   std::string input;

   //get input to change last string

   cout << "\nModify using reference\n";
   cout << "\nWhat name to use?\n";

   std::cin >> input;
   std::string s = input;

   //I can pass list to the function like that:
   funcWithList(aList, s);

   cout << "\nPrint from main, Last Element :" << aList.back();

   cout << "\nModify using Deletion\n";
   cout << "\nWhat new name to use?\n";

   std::cin >> input;
   std::string s1 = input;

   replacementForList(aList, s1);

   cout << "\nPrint from main, Last Element :" << aList.back();


   getchar();
   return 0;
}

void funcWithList(list<string>& aList, const std::string & s)
{
   //In place Replacement
   aList.assign(aList.size() - 1, s);
}

void replacementForList(list<string>& a_list, const std::string & s)
{
   //Remove the last element and replace
   a_list.pop_back();
   a_list.push_back(s);
}

OUTPUT:


Related Solutions

Write a C++ program that will make changes in the list of strings by modifying its...
Write a C++ program that will make changes in the list of strings by modifying its last element. Your program should have two functions: 1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value. 2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and inserting...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
Write a program that removes a target item (a string) from a list of strings, and...
Write a program that removes a target item (a string) from a list of strings, and that will print out the list without the item. Do not use built-in functions other than input, print, and append. Instead, you may generate a new list that does not include the target item and you can use the append() function. Your program should read two inputs: the target and the list. For example, if the input is “apple” and the list is [‘berry’,...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
Write a program that uses Python List of strings to hold the five student names, a...
Write a program that uses Python List of strings to hold the five student names, a Python List of five characters to hold the five students’ letter grades, and a Python List of four floats to hold each student’s set of test scores. The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average....
Write a Python program containing a function named scrabble_sort that will sort a list of strings...
Write a Python program containing a function named scrabble_sort that will sort a list of strings according to the length of the string, so that shortest strings appear at the front of the list. Words that have the same number of letters should be arranged in alphabetical order. Write your own logic for the sort function (you may want to start from some of the existing sorting code we studied). Do NOT use the built-in sort function provided by Python....
Programming lang C++ Write a program that reads 10,000 words into an array of strings. The...
Programming lang C++ Write a program that reads 10,000 words into an array of strings. The program will then read a second file that contains an undetermined number of words and search the first array for each word. The program will then report the number of words in the second list that were found on the first list.
Please in C++ language Write a program that reads 10,000 words into an array of strings....
Please in C++ language Write a program that reads 10,000 words into an array of strings. The program will then read a second file that contains an undetermined number of words and search the first array for each word. The program will then report the number of words in the second list that were found on the first list.
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output. Assignment: It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory. You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more...
Create an array of ten strings. Write a program to join and split these strings. Feel...
Create an array of ten strings. Write a program to join and split these strings. Feel free to use any whitespace character. Explain how the program works, when to use Python arrays, and when to use tuples, dictionaries, and sets. Explain how the program works and how it can be utilized to support an organization’s requirements.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT