I am trying to do this assignment:
Write a program that reads a list of concepts/strings from the LIST.txt text file (one concept per line), stores them into an array of strings, and then prints the following:
1.Prints a heading “LIST1:” and then prints all the concepts from the array, one concept per line
2.Prints a heading “LIST2:” and then prints all the concepts from the array that do not contain spaces, one concept per line
3.Prints a heading “LIST3:” and then prints all the concepts from the array that ends with the letter “A”, one concept per line
4.Prints a heading “LIST4:” and then prints all the concepts from the array that contain “of” (in any cases), one concept per line
5.Prints a heading “LIST5:” and then prints all the concepts from the array that contains more than one spaces, one concept per line
6.Prints a heading “LIST6:” and then prints all the concepts from the array that start with a consonant, one concept per line
7.Prints a heading “LIST7:”, sorts the array using the selection sort method from the textbook or practice exercises1and prints all the concepts from the array in alphabetical order, one concept per line
The list is:
UNITED STATES OF AMERICA
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
District Of Columbia
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Ohio
Oklahoma
Oregon
Pennsylvania
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming
American Samoa
District of Columbia
Guam
Northern Mariana Islands
Puerto Rico
United States Virgin Islands
The problem I am having is for list 3. When trying to test the program I get the debugging error "expression: string subscript out of range"
This is my code
#include
#include
#include
using namespace std;
int main()
{
ifstream file("LIST.txt");
int i = 0;
string listArray[500];
char str[255];
if (file.is_open())
{
while (file)
{
file.getline(str, 300);
listArray[i] = str;
i++;
}
cout << endl << "List 1 " << endl <<
endl;
for (int j = 0; j < i; j++)
cout << listArray[j] << "\n";
}
cout << endl << "List 2" << endl <<
endl;
for (int j = 0; j < i; j++)
{
if (listArray[j].find(' ') == string::npos)
cout << listArray[j] << endl;
}
cout << "List 3" << endl << endl;
for (int j = 0; j < i; j++) -This is the section I get
an error
{
int n = listArray[j].size() -1;
if (listArray[j][i] == 'a' || listArray[j][i] == 'A')
cout << listArray[j] << endl;
}
}
What can I do to fix it?
I have made the change and formatted it in bold.
C++ Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream file("LIST.txt");
int i = 0;
string listArray[500];
char str[255];
if (file.is_open())
{
while (file)
{
file.getline(str, 300);
listArray[i] = str;
i++;
}
cout << endl << "List 1 " << endl <<
endl;
for (int j = 0; j < i; j++)
cout << listArray[j] << "\n";
}
cout << endl << "List 2" << endl <<
endl;
for (int j = 0; j < i; j++)
{
if (listArray[j].find(' ') == string::npos)
cout << listArray[j] << endl;
}
cout << "List 3" << endl << endl;
for (int j = 0; j < i; j++) //This is the section I get an
error
{
int n = listArray[j].size() -1; //getting the size of the
string
if (listArray[j][n] == 'a' || listArray[j][n] == 'A')
//changed the index from 'i' to 'n' which is the index of last
element of the string in the list
cout << listArray[j] << endl;
}
}
Sample Ooutput:
List 1
UNITED STATES OF AMERICA
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
District Of Columbia
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Ohio
Oklahoma
Oregon
Pennsylvania
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming
American Samoa
District of Columbia
Guam
Northern Mariana Islands
Puerto Rico
United States Virgin Islands
List 2
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
Ohio
Oklahoma
Oregon
Pennsylvania
Tennessee
Texas
Utah
Vermont
Virginia
Washington
Wisconsin
Wyoming
Guam
List 3
UNITED STATES OF AMERICA
Alabama
Alaska
Arizona
California
District Of Columbia
Florida
Georgia
Indiana
Iowa
Louisiana
Minnesota
Montana
Nebraska
Nevada
North Carolina
North Dakota
Oklahoma
Pennsylvania
South Carolina
South Dakota
Virginia
West Virginia
American Samoa
District of Columbia
I am trying to do this assignment: Write a program that reads a list of concepts/strings...
C++ XOR two strings. I am trying to XOR two strings 00112233445566778899AABBCCDDEEFFAABB060606060606 and 4ca00fd6dbf1fb284ca00fd6dbf1fb284ca00fd6dbf1fb28 my program is giving me: SPTWPVSPT[X q'&t'!"u#'t~u"#rPTTTVVT as a result However, when i go to http://xor.pw/? and input the two hexadecimal strings in I get the result: 4cb12de59fa49d5fc439a56d172c15d7e61b09d0ddf7fd2e what am I doing wrong in my program below? // plaintext value here string pthex = "00112233445566778899AABBCCDDEEFFAABB"; // IV value here string sHex = "4ca00fd6dbf1fb28"; string PaddedPlainText = pthex + "060606060606"; //added the pad //cout<<PaddedPlainText<<endl; // the above...
== Programming Assignment == For this assignment you will write a program that reads in the family data for the Martian colonies and stores that data in an accessible database. This database allows for a user to look up a family and its immediate friends. The program should store the data in a hashtable to ensure quick access time. The objective of this assignment is to learn how to implement and use a hashtable. Since this is the objective, you...
The following program is in c++ ; I am trying to read in games from a file and when doing so I am only reading in parts of the file. It is giving me a segmentation fault error I am going to post my code below if anyone is able to help me debug the program I would greatly appreciate it. The program is too large to be submitted as a single question on here :( --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void Business::addGamesFromFile() {...
Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...
Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...
CODE ONE #include #include using namespace std; string x =" I am global"; // Global x int main() { string x = " I am local"; // Local x cout< cout<<::x< return 0; } CODE TWO #include #include using namespace std; string str = "i am global ";// global int main() { string srt = "i am local to main() ";//local to main() cout << str << "---" << ::str << "\n";// LINE 5555. int x=5; int y=6; if...
I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...
Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) { // average function , declaring variable int i; char str[40]; float avg = 0; // iterating in...
I need only one C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...