In: Computer Science
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
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: