Daily c++ problems

Pages: 12345
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str = "There", then after removing all the vowels, str = "Thr". After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel.


a little confused as to why you require using the function "substr" to remove vowels. that function creates a new string from an existing string's starting index and length specified by the programmer. i did not use this string-based library function.

anyway, here is one example;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
using namespace std;

bool Vowel(char ch)
{
	if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
		return true;
	else 
		return false;
}

void Remove(string & string1, int index)
{
	string1.erase(index, 1);
}

int main()
{
	cout << "Enter a short string, blank line to quit" << "\n" << ": ";
	string string1;
	while (getline(cin, string1) && string1.size() > 0)
	{
		string::size_type original = string1.size();
		for (int i = 0; i < string1.size(); i++)
		{
			if (Vowel(string1[i]))
				Remove(string1, i);
		}
		string::size_type altered = string1.size();
		if (original != altered)
			cout << "Revised string = "<< string1 << endl;
		else
			cout << "No vowels in string present\n";
		cout << "\nEnter another short string or a blank line to quit" << "\n" << ": ";
	}

	return 0;
}
@AARGH
Your Vowel function wont work in all cases.
Hi guys,

What about this design:

1
2
3
//convert each char to upper case, makes testing easier
//If the char is not  a vowel, write it to the output string. That is leave the original string untouched,
//no remove/ erase functions required. 


Edit:

Forget that - A good quality for a C++ coder is being able to read properly ........ (;-<\)
Last edited on
Your Vowel function wont work in all cases.


yes you are correct. it requires converting the string to upper or lower case before it's evaluated to ensure it works. i actually forgot to implement that.

edit; hehe, or just keep it simpler and add the capital vowels in the if statement.
Last edited on
Updated
Hello!

Thanks for the challenges thus far, loving them!

I tried my hand at the test scores one but have ran into some problems at the final step!


http://pastebin.com/QgAWrcJJ

console:
http://i.imgur.com/5tfXj.png

There is my code,

Problems faced:

- My scoring system does not add up properly unless they get full marks, it's always off by one or two when I've manually checked it.

- Minus score is a possibility, where in my code can I slot in a if(score < 0) { score = 0} without harming the integrity of other scores?


Thanks a lot! Learnt so much from this problem!

Last edited on
Really good exercises. Keep on doing this and keep them this short. Cause long exercises are just boring if its not a real project.

Really good job, this is helping me!
@speakon

Problem 1: Read this section of your code again, what's actually in foundSubString, and what's in found?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

            if(foundSubString[x] == answers[x])
            {

                score += 2;

            }
            else if(found[x] != answers[x] && found[x] != ' ')
            {

                score -=1;

            } else if(found[x] = ' ') {

                //no score change
            }


Problem 2: Let the students score negative! If you're nicer than me, you can put the if statement anywhere after the scores have been fully calculated - The score for student i is known after the loop ending line 142.
Hey Kev, thanks for your reply:


foundSubString consists of the TFTFTF string from the text file

and found = STUDENT ID TFTFTFTFTF

I put in some cout debugging code and it comes back correct, I really am baffled as to where this miscalculation is occuring!


Cheers for fixing the second problem! Appreciate it.



Just finished off the roman numerals one:

http://pastebin.com/g7rcivfh

Would really appreciate as much feed back as possible, my first attempt at using classes with C++!

Thanks
Last edited on
Think you may be a bit blinded from staring at it!

and found = STUDENT ID TFTFTFTFTF

Ok, so based on the fact x runs from 0 through to 19, what does this line mean

else if(found[x] != answers[x] && found[x] != ' ')
@ Kev

Arghh! So frustrating haha, swapped it back to foundSubString and it works perfectly.

Coding should not be done after 9 hour shifts >.o

Really did need some fresh eyes on the code.

Appreciate your time to help.
The history teacher at your school needs help in grading a true/false test. The students' IDs and test answers are stored in a text file. The first entry in the file contains answers to the test in the form:

TFFTFFTTTFFTFTFTFTT

Every other entry in the file is the studentID, followed by a blank, followed by the student's responses. For example, the entry:

ABC54301 TFTFTFTT TFTFTFFTTFT

Indicated that the student ID is ABC 54301 and the answer to question 1 is true, the answer to question 2 is false, and so on. This student did not answer question 9. The exam has 20 questions, and the class has more than 150 students. Each correct answer is awarded two points, each wrong answer gets one point deducted and no answer gets zero points. Write a program that processes the test data. The output should be the student's ID, followed by the answers, followed by the test score, followed by the test grade. Assume the standard grade scale (90-100 -- a) (80-89 -- b) etc..etc..


here goes with one exception to only create a txt file with a few entries and not 150. i also assume that the only libraries we are allowed to use is iostrem and fstream and use the built-in arrays. so no string library, etc.

text file must be present for this to work.
text file consists of;
TFFTFFTTTFFTFTFTFTTF
jesus TFFTFFTTTFFTFTFTFTTF
James TFTTTFTFTTFTFTFFTTFT
Jones TFTTTFTFTFFFTFFTTF T
Joojo TFFTTFFTFFFFF FTFTT
Jayjy TFFTFTTTTFFFFTFTTTTF



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	char* data = "text.txt"; //use to open a text file
	char student_info[50]; //holds student ID
	char student_answers[20]; //stores students' answers
	char answers[20]; //holds correct answers and used to compare with students' answers
	double score = 0; //counts the students' score
	const double max_score = 40; //sets the max score possible and used for calculations
	char grade; //stores a letter grade used for output

	ifstream in(data); //text.txt must be present in same folder as this ccp file or program will not run correctly or crash.
	in >> answers;
	cout << "Processing student Data;" << "  " << answers << " = Correct answers.\n";

	while (in >> student_info)
	{
		in.get();
		in.getline(student_answers, 21);
		for (int i = 0; i < 20; i++)
		{
			if (student_answers[i] == answers[i])
				score += 2;
			else if (student_answers[i] == ' ')
				;
			else
				--score;
		}
        double grade_scale = score / max_score * 100;
        if (grade_scale > 89)
            grade = 'A';
        else if (grade_scale > 79 && grade_scale < 90)
            grade = 'B';
        else if (grade_scale > 69 && grade_scale < 80)
            grade = 'C';
        else if (grade_scale > 59 && grade_scale < 70)
            grade = 'D';
        else if (grade_scale > -40 && grade_scale < 60)
            grade = 'F';
		cout << endl << student_info << "\t" << student_answers << "\t" << score << " " << grade_scale << "% " << grade << endl;
		score = 0;
	}
    char p; // used for gracefully exiting program
    cout << "\nDone.  Press enter key to quit."; //trying to stay platform-independant
    cin.get(p);



	return 0;
}
Updated
@Need4Sleep
I don't think a circle should inherit from a point. A circle is composed of points.
Write a program to help a local restaurant automate its breakfast billing system. The program should do the following:
a. Show the customer the different breakfast items offered by the restaurant
b. Allow the customer to select more than one item from the menu.
c. Calculate the bill

Assume the restaurant offers the following breakfast items:
Plain Egg.............1.45
Bacon and Egg....2.45
Muffin..................0.99
French Toast.......1.99
Fruit Basket.........2.49
Cereal.................0.50
Tea......................0.75

Define a struct menuItemType with two components to hold the name and price of an item. Use an array, menuList, of the menuItemTypes. Your program must contain at least the following functions:
[] Function GetData: This function loads data into the array
[] Function ShowMenu: Displays items offered by restaurant and tells user how to select items.
[] Function printCheck: calculates and prints check(Note that the billing amount should include a 5% tax)

Format your output with 2 decimal places. The name of each item in the output must be left justified. You may assume that the user selects only one of a particular item.



got a tad lazy and skipped the left-justification part but rest should be good to go.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>

using namespace std;
struct menuItemType
{
	string name;
	double price;
};

void GetData(menuItemType & menu, string name, double price)
{
	menu.name = name;
	menu.price = price;
}

void ShowMenu()
{
	cout << "(1) Plain Egg.............1.45\n"
			<< "(2) Bacon and Egg....2.45\n"
			<< "(3) Muffin..................0.99\n"
			<< "(4) French Toast.......1.99\n"
			<< "(5) Fruit Basket.........2.49\n"
			<< "(6) Cereal.................0.50\n"
			<< "(7) Tea......................0.75\n";
}

void printCheck(menuItemType& selection, double* total)
{
	*total += selection.price;
	cout << selection.name << " " << selection.price << endl;
}

int main()
{
	menuItemType menuList[50];
	cout << "Select up to 50 items by their menu numbers.  Press s then enter key to stop\n";
	int choice;
	int counter = 0; // used to ensure array does not run out of bounds
	double price; // price for each menu item added to the array
	double total = 0; // running total
	string name; // name for each menu item added to the array
	menuItemType * ptr_begin(menuList); // pointers will be used to manual figure out the size of the array (how many elements have valid values in them)
	menuItemType * ptr_end(menuList); // end pointer will iterate down the array to mark where the end would be

	ShowMenu();
	while (cin >> choice)
	{
		ShowMenu();
		if (choice == 1)
		{
			price = 1.45;
			name = "Plain Egg";
		}
		else if (choice == 2)
		{
			price = 2.45;
			name = "Bacon and Egg";
		}
		else if (choice == 3)
		{
			price = 0.99;
			name = "Muffin";
		}
		else if (choice == 4)
		{
			price = 1.99;
			name = "French Toast";
		}
		else if (choice == 5)
		{
			price = 2.49;
			name = "Fruit Basket";
		}
		else if (choice == 6)
		{
			price = 0.50;
			name = "Cereal";
		}
		else if (choice == 7)
		{
			price = 0.75;
			name = "Tea";
		}
		else // if user entered an improper selection number.
		{
			cout << "\n***Thats not a proper selection.  Please try again.***\n";
				continue;
		}
		GetData(menuList[counter], name, price);
		ptr_end += 1;
		++counter;
		if (counter == 50) // user entered maximum selection of menu items.
			break;
	}
	if (ptr_begin != ptr_end) // if pointers point to different areas of array then we know user chose something to order and will display the bill.
	{
		cout << "\nYou selection consists of;\n";
		for (menuItemType * ptr_begin(menuList); ptr_begin != ptr_end; ptr_begin += 1)
		{
			printCheck(*ptr_begin, &total);
		}
		cout << "\nYour total bill including 5% tax will be: $";
		cout.precision(3);
		cout << total * 1.05 << endl;
	}
	cout << "\nThank you.  Come again.\n";
	char f; // use to prevent console from closing down.  may not work on MS visual C++ compiler for some reason.
	cin >> f;
	//system("pause"); //use for MS visual C++ to prevent console from closing down too fast.
	return 0;
}
The circle one was so fun but pretty hard to make, one of my best programming exercises!

Thank you for this. Please keep it up!
Glad you enjoyed it! i personally love those types of problems that include inheritance. You'll be seeing a lot more of those types of problems in the future.
Last edited on
Actually the problem for me was to solve the circle algoritm. I've worked alot with inheritance already.
Question to Need4Sleep:

Will you erase 'old' assignments as you keep making new ones?
please don't... :)

Question to Need4Sleep:

Will you erase 'old' assignments as you keep making new ones?
please don't... :)


I designed the post so that new users coming to the forums could see this and use the post for their daily c++ needs, regardless of how old the post might be.

Also, updated and in need of some help from you guys for the question difficulty. Happy programming!
Pages: 12345