Okay, I've figured it out. My program is now working. Thanks!
Okay, I've figured it out. My program is now working. Thanks!
Okay, here is what I have now. It doesn't wait now after entering the input/output files, but it doesn't put down the emails. It has a termination error.
"terminate called throwing an exceptionAbort trap: 6"
while(getline(fin,emails))
{
while(atSymbol(emails[i]) == true)
{
i = 0;
i++;
if(emails[i] == '@') break;
}
int s = i;
while(isValidCharacter(emails[s]) == true)
{
s--;
if (s < 0) break;
if (isValidCharacter(emails[s]) == false)
{
s++;
if(isValidCharacter(emails[s]) == true) break;
else if(s == i) break;
}
}
bool hasDot = false;
int e = i;
while(isValidCharacter(emails[e]) == true)
{
e++;
if(e == emails.length()) break;
if(isValidCharacter(emails[e]) == false) break;
if(emails[e] == '.')
hasDot = true;
}
string anEmail = emails.substr(s,e-s);
if((s<i) && (e>i) && (hasDot == true))
cout << emails << endl;
}
I'm looking for valid characters, is that what I need to put in the while loop statement? Otherwise, I'm not sure I'm understanding completely.
Like this? It is 3 separate loops now.
while(getline(fin,emails))
{
while(i < emails.length())
{
i = 0;
i++;
if(emails[i] == '@') break;
}
int s = i;
while(s > 0)
{
s--;
if (s < 0) break;
if (isValidCharacter(emails[s]) == false)
{
s++;
if(isValidCharacter(emails[s]) == true) break;
else if(s == i) break;
}
}
bool hasDot = false;
int e = i;
while(e != emails.length())
{
e++;
if(e == emails.length()) break;
if(isValidCharacter(emails[e]) == false) break;
if(emails[e] == '.')
hasDot = true;
}
if((s<i) && (e>i) && (hasDot == true))
cout << emails << endl;
}
}
I changed my loops a bit to what I could understand from everyone. The program compiles still, and runs. It is still waiting after the files are entered. It lists my input and output file but no emails.
This is what I changed the loops to:
string emails;
int i;
while(getline(fin,emails))
{
for(i = 0; i < emails.length(); i++)
{
if(emails[i] == '@')
{
int s = i;
while(true)
{
if (s < 0) break;
if (isValidCharacter(emails[s]) == false)
{
s++;
if(isValidCharacter(emails[s]) == true) break;
else if(s == i) break;
}
}
bool hasDot = false;
int e = i;
while(true)
{
if(e == emails.length()) break;
if(isValidCharacter(emails[e]) == false) break;
if(emails[e] == '.')
hasDot = true;
}
if((s<i) && (e>i) && (hasDot == true))
cout << emails << endl;
}
}
}
I'm doing a program, and trying to find the @, going back to the beginning to search for valid characters, and when I find an invalid one I move it forward to valid one. Same goes for the end, except just leaving it at the invalid character. My program compiles, however after it takes the input and output files, it just sits there. Nothing appears after it tells me my input and output files. I can't figure out why.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class toLower{public: char operator() (char c) const {return tolower(c) ;}};
bool isValidCharacter(char c)
{
bool validCheck = false;
if ((c>='A' && c<= 'Z') || (c>='a' && c<='z') || (c>='0' && c<='9') || (c=='.') || (c=='-') || (c=='+'))
validCheck = true;
return validCheck;
}
int main()
{
ifstream fin;
string inputFile;
string outputFile;
string defaultInput = "fileContainingEmails.txt";
string defaultOutput = "copyPasteMyEmails.txt";
cout << "Enter input filename[default: fileContainingEmails.txt]: ";
getline(cin, inputFile);
if (inputFile.length() == 0)
{
inputFile = defaultInput;
outputFile = defaultOutput;
cout << "Enter output filename[default: copyPasteMyEmails.txt]: ";
getline(cin,outputFile);
if (outputFile.length() == 0)
outputFile = defaultOutput;
}
else
{
string c;
cout << "Enter output filename[default: " << inputFile << "]: ";
getline(cin, c);
if (c.length() == 0)
outputFile = inputFile;
else
outputFile = c;
}
cout << "Input file: " << inputFile << endl;
cout << "Output file: " << outputFile << endl;
fin.open(inputFile.c_str());
if (!fin.good()) throw "I/O error";
string emails;
int i;
while(getline(fin,emails))
{
for(i = 0; i < emails.length(); …
I'm not sure what I'm doing wrong. I get a warning when I compile and an error after my program runs.
This is the warning I get:
myList2.cpp: In function ‘int main()’:
myList2.cpp:96: warning: deleting array ‘int score [6]’
And this is what shows after my program runs:
a.out(7023) malloc: *** error for object 0x7fff6bae4bf0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
And this is my code:
#include <string>
#include <iostream>
using namespace std;
double getAverage(int* score, int n)
{
int sum = 0;
int c = 0;
for (c = 0; c < n; c++)
sum += score[c];
double average = double(sum) / n;
return average;
}
int compare(const void* pa, const void* pb)
{
const int& a = *static_cast<const int*>(pa);
const int& b = *static_cast<const int*>(pb);
if (a < b) return -1; // negative if a<b
if (a > b) return 1; // positive if a>b
return 0; // 0 for tie
}
bool hasAScore(int size,int score[])
{
int d;
for (d=0; d < size; d++)
{
if (score[d] >= 90)
{
cout << "At least one 'A' grade is present" << endl;
return true;
}
}
cout << "No A grades present" << endl;
return false;
}
int main()
{
// create empty list
const int MAX_SCORES = 6;
int nScores = 0;
int score[MAX_SCORES];
// user input for amount of scores
int size;
cout << "How many scores? "; …
Thank you, figured it out with the library.
I started this code, and it does encode the text, however it only does the first line of text and repeats it. It is also encoding spaces.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream fin;
string fileName;
cout << "What file do you want to use for input? ";
getline(cin, fileName);
fin.open(fileName.c_str());
string s;
while (true)
{
if (!fin.good()) break;
getline(fin, s);
string s = "Hello, World"; // an example
for (int i = 0; i < s.length(); i++) // for each char in the string...
s[i]++;
cout << s << endl;
}
fin.close();
return 0;
}
That makes it much cleaner, however I had to do it a certain way, but thank you!
I didn't even realize I was doing it that way. Thank you, my program is working now.
Thanks, it does make it a lot easier to read. Still need help with why it won't print the outcome though.
Thank you! That fixes that, however I rewrote my the coding, and now have a new problem. I haven't added the loops in yet, but I will. It compiles and runs fine. However, it only prints the human and computer choices, not the outcome. I can't figure out why.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int userChoice()
{
char choice;
int choice2;
do
{
cout << "Please enter your choice[r,p,s,q]: ";
cin >> choice;
cout << "Human: " << choice;
}while ((choice != 'R')&&(choice != 'r')&&(choice != 'P')&&(choice != 'p')&&(choice != 'S')&&(choice != 's'));
if(choice == 'R' || choice == 'r')
choice2 = 0;
if(choice == 'P' || choice == 'p')
choice2 = 1;
if (choice == 'S' || choice == 's')
choice2 = 2;
if (choice == 'Q' || choice == 'q')
choice2 = 3;
return choice2;
}
int computerChoice()
{
srand(time(0));
char compChoice = rand() % 3;
if (compChoice == 0)
cout << " Computer: R" << endl;
if (compChoice == 1)
cout << " Computer: P" << endl;
if (compChoice == 2)
cout << " Computer: S" << endl;
return compChoice;
}
int printOutcome(int choice, int compChoice)
{
if (choice == '0' && compChoice == '0')
cout << "Tie \n";
if (choice == '0' && compChoice == '1')
cout << "Computer wins\n";
if (choice == '0' && compChoice == '2')
cout << "Human wins\n";
if (choice == '1' && compChoice == '0')
cout << "Human wins\n";
if (choice == '1' && compChoice == …
In my printScore, I have to check for q? Or can I just move all the user input into the humanChoice function.
I took the humanChoice out of main, and left it in printScore. However, now when I enter q it says invalid choice.
Oh, wow. That fixed that issue. Thanks.
Because I have
choice == 'Q' || 'q' break;
Is that what is causing it so my program can not exit with q?
I have it all written, and it works. However, I have to enter a value and it brings the prompt up again. When I enter the value in the second time, it plays the game. I always have to enter the value twice to get it to work. I'm not sure what is going wrong.
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int computerChoice()
{
int compChoice = rand() % 3;
return compChoice;
}
char humanChoice()
{
char choice;
while(true)
{
// prompt for, and read, the human's choice
cout << "Choose(Rock,Paper,Scissors,or Quit): ";
cin >> choice;
cin.ignore(1000, 10);
// if human wants to quit, break out of loop
if (choice == 'Q' || 'q') break;
if (choice == 'R' || choice == 'r') break;
if (choice == 'S' || choice == 's') break;
if (choice == 'P' || choice == 'p') break;
}
return choice;
}
void printScore()
{
char human;
int computer;
human = humanChoice();
computer = computerChoice();
// human choice
if (human == 'R' || human == 'r')
cout << "Human: R, ";
else if (human == 'S' || human == 's')
cout << "Human: S, ";
else if (human == 'P' || human == 'p')
cout << "Human: P, ";
else cout << "Invalid Choice" << endl;
// computer choice
if (computer == 0)
cout << "Computer: R, ";
else if (computer == 1)
cout << "Computer: P, ";
else if (computer == 2)
cout << "Computer: S, ";
// determine winner
if …
I just have a quick question, I tried to figure this out from my textbook but can't seem to find it.
j = 5, i =7
Evaluate
(++j == i) + 3
How does C evaluate the expression? To me, it just doesn't make sense.
I'm not looking for the answer to this, but more of how it works in general.
I went back and figured that out and saw that I needed to make it a + and not a - when I was using strlen(x1)+1
I just need to change the - to a + and it works. Thanks everyone!
Okay, I did that and they are being read properly. I think it has something to do with when I add the null character, and it is taking away that last letter and replacing it with a null one.
How do I properly add a null character?
I got the garbage letters to go away, however it is cutting off the last letter when I run the program. Do I need to make the array bigger? Or is something else going wrong?
Enter two words: program problem
The longest common prefix of progra and proble is pro
#include <stdio.h>
#include <string.h>
void prefix(char x1[], char x2[])
{
char x3[254];
int a = 0;
int b = 0;
int c = 0;
a = strlen(x1);
b = strlen(x2);
int i;
if (a < b)
c = a;
else
c = b;
for(i = 0; i < c; ++i)
{
if(x1[i] == x2[i])
x3[i] = x1[i];
else break;
}
printf("%s\n",x3);
}
int main(void)
{
char x1[254];
char x2[254];
int i;
for(i = 0; i < 1; i++)
{
printf("Enter two words: ");
scanf("%s %s",x1,x2);
x1[strlen(x1)-1] = '\0';
x2[strlen(x2)-1] = '\0';
printf("The longest common prefix of %s and %s is ", x1,x2);
prefix(x1,x2);
}
return 0;
}
Nevermind! I need to just declare i and put i = 0 in my for loop conditions. And it is fixed!
The program runs and compiles. When I enter the two words, it gives me a bunch of random stuff when printing.
Enter two words: economy ecology
The longest common prefix of and § is hA(~·
Okay, I understand that. I took the i out and declared it outside the loop. However, now that I did that to both my for loops, it compiles, but when I run the program it is just blank. It doesn't prompt the user anymore.
int i = 0;
for(i < c; ++i;)
{
while(x1[i] == x2[i])
x3[i] = x1[i];
if (x1[i] != x2[i])
break;
}
I actually fixed the first error. But now I am getting:
prefix.c: In function 'prefix':
prefix.c:25:9: error: 'for' loop initial declarations are only allowed in C99 mode
prefix.c:25:9: note: use option -std=c99 or -std=gnu99 to compile your code
prefix.c:28:31: warning: assignment makes integer from pointer without a cast [enabled by default]
prefix.c: In function 'main':
prefix.c:41:2: error: 'for' loop initial declarations are only allowed in C99 mode
I didn't see that it was a semicolon. I changed it but I still get the same error. I don't know what you mean by rogue opening paren.
void prefix((char x1[],char x2[]))
Plus I get this error, I don't know what this means:
prefix.c:41:2: error: 'for' loop initial declarations are only allowed in C99 mode
prefix.c:41:2: note: use option -std=c99 or -std=gnu99 to compile your code
What do you mean?
I am trying to find the longest common prefix of two words entered by the user. I don't understand what is going wrong. I keep getting this error when I compile:
prefix.c:11:13: error: expected declaration specifiers or '...' before '(' token
And this is my code:
#include <stdio.h>
#include <string.h>
#define MAXFORTHIS 100
void prefix((char x1[]; char x2[])
{
char x3[MAXFORTHIS];
int a = 0;
int b = 0;
int c = 0;
a = strlen(x1);
b = strlen(x2);
if (a < b)
c = a;
else
c = b;
for(int i = 0; i < c; ++i)
{
while(x1[i] == x2[i])
x3[i] = x1;
if (x[i] != x2[i])
break;
}
printf("%s",x3);
}
int main(void)
{
char x1[MAXFORTHIS];
char x2[MAXFORTHIS];
for(int i = 0; i < 5; i++)
{
printf("Enter two words: ");
scanf(x1,x2,MAXFORTHIS,stdin);
x1[strlen(x1)-1] = '\0';
x2[strlen(x2)-1] = '\0';
printf("The longest common prefix of %s and %s is ", x1,x2);
prefix(x1,x2);
}
return 0;
}
I'm trying to search a text file for any email addresses. I'm trying to get it to print the line that every @ sign appears in. However, it is only printing the @ sign instead of the whole line. And I don't understand why.
//gathering emails
string Emails;
int i;
while (getline(fin,Emails))
{
for (i = 0; i < Emails.length(); i++)
{
if (Emails[i] == '@')
{
cout << Emails[i] << endl;
}
}
}
I figured out how to do it but thank you!
No, that doesn't really touch on what I am looking for.
I'm just wondering if someone could point me to some sources on linked lists. I'm trying to make it so the user inputs how many they want to add to the list and the list will stop after that amount. I'm just looking for some examples on it to help me understand how I would input it in my program.
I don't know why I had the first bit.
I have to have that include because I am gathering from a file and its required. I use elements from the library.
What I'm saying is there are about 9 names in the text file, but it is only printing 5 names even though the max is 8. I don't understand why it is cutting off sooner.
That is the only bit of std I've used. What he suggested isn't in my textbook.
I've fixed the issue however now it is only doing 5 names and sorting them. I have the capacity set to 8 and more than 8 are in the list.I don't understand why it is not doing the full 8.
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
struct Student
{
string name;
};
void printNames(Student* student, int nStudents)
{
int i;
for (i = 0; i < nStudents; i++)
{
cout << student[i].name << endl;
}
}
int main()
{
ifstream fin;
// opening file
string fileName;
cout << "What file do you want to open?: ";
getline(cin, fileName);
fin.open(fileName.c_str());
if (!fin.good()) throw "I/O error";
// creating empty list
const int MAX_NAMES = 8;
int nStudents = 0;
Student student[MAX_NAMES];
// read and save names
while (fin.good())
{
Student aStudent;
getline(fin, aStudent.name);
fin.ignore(1000, 10);
// add name to list if not full
if (nStudents < MAX_NAMES)
student[nStudents++] = aStudent;
}
fin.close();
for (int i =0; i < nStudents; i++)
{
for (int j = i + 1; j < nStudents; j++)
{
if (student[i].name > student[j].name)
{
Student temp = student[i];
student[i] = student[j];
student[j] = temp;
}
}
}
printNames(student, nStudents);
return 0;
}
They all involve std, and we have not done that in my class so far. In the text book example of sorting with names, int is used which is why I used it as well.
First off, I know I have a few other errors besides the sorting code. I am working on figuring those out as well. I need to sort the names in the file alphabetically. I did put a code which I thought would work, but doesn't. Lines 39 through 50.
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream fin;
// opening file
string fileName;
cout << "What file do you want to open?: ";
getline(cin, fileName);
fin.open(fileName.c_str());
if (!fin.good()) throw "I/O error";
// creating empty list
const int MAX_NAMES = 8;
int aName = 0;
int names[MAX_NAMES];
// read and save names
while (fin.good())
{
// read from file
int aName;
getline(fin, aName);
// skip blank lines
if (aName.length() > 0)
{
// add name to list if not full
if (aName < MAX_NAMES)
names[aName++] = aName;
}
}
for (int i =0; i < aName; i++)
{
for (int j = i + 1; j < aName; j++)
{
if (names[i] > names[j])
{
names temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
cout << aName << endl;
fin.close();
return 0;
}
Thank you, I didn't notice that. And thanks for the other help. My program is running just how I needed it to.
I don't understand the last part of what you said for the MAX_STUDENTS. Currently when I want it to input 6 students, it lets me input 6 students. And it prints all 6 by their gpas.
Expanding my for loop to include the adding a record has solved the aStudent problem.
The problem I'm having now is I the output to be on all one line. I thought I made it that way in my void function but it prints the name on one line, goes to the next and prints the id and gpa.
This is the void function
void printStudents(Student* student, int nStudents)
{
int i;
for (int i = 0; i < nStudents; i++)
{
cout << "Name: " << left << setw(30) << student[i].name << endl;
cout.fill('0');
cout << " ID = " << right << setw(7)
<< student[i].id << ", gpa = "
<< student[i].gpa << endl;
cout.fill(' ');
}
}
And the output looks like this:
Name: Brittany
ID: 54754623, GPA: 3.42
But I want it like this:
Name: Brittany ID: 4356346, GPA: 3.23
I'm making a student database using array based lists. I'm having trouble with making the list size created by the user. I have a for loop there now but once I put it there, I get an error saying aStudent is an undeclared variable and I don't understand why. There wasn't an error before the for loop.
int* stud = new int[size];
// creating a record
int b;
for (int b = 0; b < size; b++)
{
Student aStudent;
cout << "Student's name? ";
getline(cin, aStudent.name);
cout << "Student's ID? ";
cin >> aStudent.id;
cin.ignore(1000, 10);
cout << "Student's GPA? ";
cin >> aStudent.gpa;
cin.ignore(1000, 10);
}
// skipping separator
cin.ignore(1000, 10);
// adding record to list
if (nStudents < MAX_STUDENTS)
student[nStudents++] = aStudent;
// sorting by gpa
for (int i = 0; i < nStudents; i++)
{
for (int j = i + 1; j < nStudents; j++)
{
if (student[i].gpa > student[j].gpa)
{
Student temp = student [i];
student [i] = student[j];
student [j] = temp;
}
}
}
printStudents(student, nStudents);
cout << endl;
return 0;
}
Nevermind I figured it out. It works properly now. Thank you!
I don't understand what you mean. Do I put that in when I bring up my program and it isn't waiting for input?
Oh, okay. That is what I was doing with it. It works, however when it asks about the 2nd and 3rd student, it puts the first two questions together and only gets the answer for the 2nd one.
So without an array I have to create 3 functions?
Yeah, I get it. I've chosen not to use arrays in this simple one because it is only 3 students and I've been able to keep track of it easily.
I do have one final question. How do I take my questions and put them into a void function?
I tried this
void questionsstudent(const Student& SItem)
{
cout << "What is Student 1's name?: ";
}
But if I put in the code from below it will only go for the first student. How do I make it generic? I know I can't just leave it as name because that makes it an undeclared variable.
getline(cin,a.name);
I'm still getting an error. It says my variables are undeclared, the ones I have so far in the print function. I want it to use the variables from struct Student. How do I do that? I want it to be able to use a generic one for 3 students. I was trying it with 'a' first to see how it works.
I'm trying to create a print void function, however I'm not understanding how to call the database in the print function. I've tried to do something similar to what is in my textbook but all I'm getting is errors. This is what I've done so far.
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
struct Student
{
string name;
string address;
string city;
string state;
int zip;
char gender;
string id;
float gpa;
} a, b, c;
void printstudent(Student& a)
{
cout << name << endl;
cout << address << endl;
}
int main()
{
// student 1 user input info
cout << "What is Student 1's name?: ";
getline(cin,a.name);
cout << "What is Student 1's address?: ";
getline(cin,a.address);
cout << "What is Student 1's city?: ";
getline(cin,a.city);
cout << "What is Student 1's state?: ";
getline(cin,a.state);
cout << "What is Student 1's student ID?: ";
getline(cin,a.id);
cout << "What is Student 1's zip?: ";
cin >> a.zip;
cout << "What is Student 1's gender?[M/F]: ";
cin >> a.gender;
cout << "What is Student 1's gpa?: ";
cin >> a.gpa;
printstudent(Student& a);
return 0;
}
Thanks. I didn't even realize that.
I'm trying to get what I put in the value for the database to output, however whenever I use a cout statement with the a.name I get an error on line 21 which is the line with the cout statement. I've done it the way it is in the examples of my textbook so I don't understand what is going wrong.
#include <iomanip>
#include <iostream>
using namespace std;
struct Student
{
string name;
string address;
string city;
string state;
string zip;
string gender;
string id;
float gpa;
} a, b, c;
int main()
{
a.name = "John";
cout << a.name;
cout << "Press ENTER to continue..." << endl;
cin.get();
return 0;
}