Blank answers dynamic array - c++

Been given a task to change my code to dynamic array.
The program asks you to input array size 1 to 100. After that input vowels 1 by 1 with the normal array teikums[100]; it outputted the vowels and the vowel amount.
Been given a task to make dynamic array without the "new".
But after adding the dynamic array:
char *teikums = (char*)malloc(100);
It outputs blank space.
#pragma hdrstop
#pragma argsused
#include <string>
#include <tchar.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
int main() {
char *teikums = (char*)malloc(100);
int c, i, count, patsk; // Patskani
char yesno; // Atkartosanas Mainigais
do {
cout << " " << teikums[i];
system("cls"); // Notira Ekranu
do {
patsk = 0; // Pieskir vertibu
cout << "Input array size 1-100: ";
cin >> count;
if (count > 100 || count < 1) {
cout << "Array cant be lower or higher than 0\n";
}
} while (count > 100 || count < 1);
do {
cout << "Input " << count << "letters one by one\n";
for (i = 1; i <= count; i++) {
cin >> teikums[i];
if (!((teikums[i] >= 'a' && teikums[i] <= 'z') || (teikums[i] >=
'A' && teikums[i] <= 'Z'))) {
cout << "Error! Only input letters\n";
i = i - 1;
}
}
} while (i <= count);
cout << "\nUsed Vowels:";
for (i = 0; teikums[i] != '\0'; i = i + 2) {
if (teikums[i] == 'a' || teikums[i] == 'e' || teikums[i] == 'i' ||
teikums[i] == 'o' || teikums[i] == 'u' || teikums[i] == 'A' ||
teikums[i] == 'E' || teikums[i] == 'I' || teikums[i] == 'O' ||
teikums[i] == 'U') {
++patsk;
cout << " " << teikums[i];
teikums[i] = 0;
}
}
cout << "\nVowel ammount: " << patsk;
cout << ("\nDo you wish to continue(Y/Else):");
// prasa lietotajam vai velas atkartot
cin >> yesno;
if (yesno == 'y' || yesno == 'Y') {
}
else {
return 0;
}
} while (tolower(yesno) != 'n');
getch();
}

In this line:
cout << " " << teikums[i];
i is not initialized and therefore it contains an indeterminate value.
This is causing all your troubles. But there may be other problems elsewhere, I didn't check all details.
If it worked with char teikums[100]; it's pure coincidence.
Google "C undefined behaviour" for more information.

Related

Check if both characters are vowels or consonant and say yes, if they are different say no. Display vowels and consonants

I need to write a program where I enter 2 letters and it should display "Yes" if both letters are vowels or consonants. If they are different types it should display "NO".
At the end it should display the 2 entered vowels and consonants separately.
I've tried this code but it doesn't work for NO. Can you help me fix it?
#include <iostream>
using namespace std;
int main()
{
char a = 'a';
char b = 'b';
cout << "enter first charcter: " << endl;
cin >> a;
cout << "enter second character: " << endl;
cin >> b;
if ((a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') && (b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u'))
{
cout << "YES" << endl;
}
else if ((a != 'a' || a != 'e' || a != 'i' || a != 'o' || a != 'u') && (b != 'a' || b != 'e' || b != 'i' || b != 'o' || b != 'u'))
{
cout << "YES" << endl;
}
else if ((a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') && (b != 'a' || b != 'e' || b != 'i' || b != 'o' || b != 'u'))
{
cout << "NO" << endl;
}
else if ((a != 'a' || a != 'e' || a != 'i' || a != 'o' || a != 'u') && (b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u'))
{
cout << "NO" << endl;
}
return 0;
}
There is one issue in your second YES case and likely every consonant check. You are using logical OR, when it should be logical AND. Your character a can't be 'a' AND it cannot be 'e' AND etc. If a = 'e', it's still not the other vowels, and your big logical OR case returns true when it shouldn't.
You also repeat yourself too much. It's annoying to write, and if there is an error, like you have, it needs to be fixed in many places. Use DRY (Don't Repeat Yourself) and create a function that tells you if you have a vowel or not.
Then, you only need to explicitly check for just the YES cases or just the NO cases. Here's an example:
#include <iostream>
#include <string>
bool is_vowel(char c) {
std::string vowels = "aeiou";
return vowels.find(c) != std::string::npos;
}
int main() {
char first;
char second;
std::cout << "Letter: ";
std::cin >> first;
std::cout << "Letter: ";
std::cin >> second;
bool firstIsVowel = is_vowel(first);
bool secondIsVowel = is_vowel(second);
if ((firstIsVowel && secondIsVowel) || (!firstIsVowel && !secondIsVowel)) {
std::cout << "YES\n";
} else {
std::cout << "NO\n";
}
}
Output:
~/tmp
❯ ./a.out
Letter: a
Letter: i
YES
~/tmp
❯ ./a.out
Letter: b
Letter: c
YES
~/tmp
❯ ./a.out
Letter: a
Letter: h
NO
~/tmp took 4s
❯ ./a.out
Letter: h
Letter: a
NO
I have managed to make it work. Thank you guys!
#include <iostream>
using namespace std;
int main()
{
char a = 'a';
char b = 'b';
bool vowel1, vowel2;
cout << "enter first letter: " << endl;
cin >> a;
cout << "enter second letter: " << endl;
cin >> b;
vowel1 = (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u');
vowel2 = (b == 'a' || b == 'e' || b == 'i' || b == 'o' || b == 'u');
if ((vowel1 && vowel2) || (!vowel1 && !vowel2))
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
if (vowel1)
{
cout << "vowel: " << a << endl;
}
else
{
cout << "consonant: " << a << endl;
}
if (vowel2)
{
cout << "vowel: " << b << endl;
}
else
{
cout << "consonant: " << b << endl;
}
return 0;
}

How to fix not printing any character and just count vowels and characters and read every character in a file

I just want to read every character in a file where I put characters from A TO Z but the program prints A every time and count vowels 4 and character 25 but expectation was to printing vowels 5 and characters 26 how to fix this program fixing from last 4 hours but nothing progress?
Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
int i, count = 0, vowel_count = 0;
string file_name;
cout << "enter file name:";
cin >> file_name;
ifstream fin;
fin.open(file_name);
char ch;
while (!fin.eof()) {
fin.get(ch);
cout << ch;
while (fin >> ch) {
i = ch;
if ((i > 63 && i < 91) || (i > 96 && i < 123))
count++;
if (i == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
vowel_count++;
}
cout << "\n No. of Characters in a File : " << count;
cout << "\n No. of vowel characters in the File : " << vowel_count;
}
fin.close();
return 0;
}
You have some really minor erros in the code, which I fixed for you.
Additionally, I added a check, if the file could be opened or not. That is the problem in most cases.
Please see below:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
int count = 0, vowel_count = 0;
string file_name;
cout << "\nEnter file name: ";
cin >> file_name;
ifstream fin(file_name);
if (fin) {
char ch;
while (fin.get(ch)) {
cout << ch;
if ((ch >= 'A' && ch <= 'Z') || (ch > 'a' && ch <= 'z'))
count++;
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
vowel_count++;
}
fin.close();
cout << "\n No. of Characters in a File : " << count;
cout << "\n No. of vowel characters in the File : " << vowel_count;
}
else std::cerr << "\n\n*** Error. Could notopen '" << file_name << "'\n\n";
return 0;
}

Stack around the variable "name" was corrupted C++

I'm trying to use CStrings to do miscellaneous tasks in C++, such as remove all the vowels from the name provided. However, I can't seem to figure out why I'm getting this error:
Stack around the variable "name" was corrupted.
Why is this happening?
Here is the code:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
using namespace std;
void cStringDemo();
void stringDemo();
void removeCVowels(char myGuess[50]);
int main() {
cStringDemo();
cin.get();
cin.ignore();
}
void cStringDemo() {
char name[] = "Seth Smith";
char guess[50];
cout << "Guess my name! [First and Last, EX: Bobby Hall.]" << endl;
cin.get(guess, 20);
if (strcmp(name, guess) == 0) {
cout << "Correct!" << endl;
}
else {
cout << "Incorrect!" << endl;
}
cout << "You guessed " << guess << "." << endl;
removeCVowels(guess);
}
void removeCVowels(char myGuess[50]) {
char nameNoVowel[50];
strcpy_s(myGuess, 100, nameNoVowel);
for (int x = 0; x < 50; x++) {
if (nameNoVowel[x] == 'a' || nameNoVowel[x] == 'e' || nameNoVowel[x] == 'i' || nameNoVowel[x] == 'o' || nameNoVowel[x] == 'u' || nameNoVowel[x] == 'A' || nameNoVowel[x] == 'E' ||
nameNoVowel[x] == 'I' || nameNoVowel[x] == 'O' || nameNoVowel[x] == 'U')
{
nameNoVowel[x] = ' ';
}
}
}
This is undefined behavior:
void removeCVowels(char myGuess[50]) {
char nameNoVowel[50];
strcpy_s(myGuess, 100, nameNoVowel);
You are copying from uninitialized nameNoVowel to myGuess. You should swap the arguments of strcpy_s. Also, even if you swap the two arguments of strcpy_s, the limit of 100 is also too big, since nameNoVowel is only 50 chars. Try:
void removeCVowels(char myGuess[50]) {
char nameNoVowel[50];
strcpy_s(nameNoVowel, sizeof(nameNoVowel)-1, myGuess);
There are a few problems in the code that you posted. Below is code fixed with explanation in comments of what was wrong:
void cStringDemo() {
char name[] = "Seth Smith";
char guess[50] = {0}; //in here initialize the table with zeros
cout << "Guess my name! [First and Last, EX: Bobby Hall.]" << endl;
cin.get(guess, 20); // I am not sure why you want 20 characters and have array of size 50
if (strcmp(name, guess) == 0) {
cout << "Correct!" << endl;
}
else {
cout << "Incorrect!" << endl;
}
cout << "You guessed " << guess << "." << endl;
removeCVowels(guess);
}
void removeCVowels(char myGuess[50]) {
char nameNoVowel[50] = {0}; //it is always good to initialize variables
strcpy_s(myGuess, 50, nameNoVowel); //here lies the problem you tried to copy 100
// characters from array size of 50 this leads
//to undefined behaviour of your program and stack corruption
for (int x = 0; x < 50; x++) {
if (nameNoVowel[x] == 'a' || nameNoVowel[x] == 'e' || nameNoVowel[x] == 'i' || nameNoVowel[x] == 'o' || nameNoVowel[x] == 'u' || nameNoVowel[x] == 'A' || nameNoVowel[x] == 'E' ||
nameNoVowel[x] == 'I' || nameNoVowel[x] == 'O' || nameNoVowel[x] == 'U')
{
nameNoVowel[x] = ' ';
}
}
}

Inputting array problems

Start of the program you need to input how many elements you want for example if selected 3 you can type a b c but if you input more than 3 elements a b c d the program instantly crashes.
Haven't figured out how to make if you input the more than 4 element a b c d it will only read the a b c part.
#pragma hdrstop
#pragma argsused
#include <string>
#include <tchar.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <iostream.h>
#include <iomanip>
#include <sstream>
int main() {
char teikums[100]; // Masiva lielums
int c, i, count, patsk; // Patskani
char yesno; // Atkartosanas Mainigais
do {
system("cls"); // Notira Ekranu
patsk = 0; // Pieskir vertibu
cout << "Ievadi Massiva lielumu 1-100: ";
cin >> count;
if (count > 100 || count < 1) {
cout << "Massivs nedriklst but lielaks par 100 vai mazaks par 0";
}
else {
cout << "Ievadi " << count << " burtus vienu pa vienam\n";
for (i = 1; i <= count; i++) {
cin >> teikums[i];
}
cout << "\nIzmantotie Patskani:";
for (i = 0; teikums[i] != '\0'; i = i + 2) {
if (teikums[i] == 'a' || teikums[i] == 'e' ||
teikums[i] == 'o' || teikums[i] == 'o' ||
teikums[i] == 'u' || teikums[i] == 'A' ||
teikums[i] == 'E' || teikums[i] == 'I' ||
teikums[i] == 'O' || teikums[i] == 'U') {
++patsk;
cout << teikums[i];
}
}
cout << "\nPatskanu Skaits: " << patsk;
}
cout << ("\nVai velaties atkartot(Y/cits):");
// prasa lietotajam vai velas atkartot
cin >> yesno;
if (yesno == 'y' || yesno == 'Y') {
}
else {
return 0;
}
}
while (tolower(yesno) != 'n');
getch();
}
Your program will not behave as you expect if you enter in more numbers than your count because of how it is parsing the inputs.
for (i = 1; i <= count; i++) {
cin >> teikums[i];
}
Let's say your count is 1, and you enter in 1 2.
This will loop once and say teikum[1] to be 1, and 2 is still leftover waiting to be grabbed by an input stream. Below that you have,
cin >> yesno;
which will now set yesno to be 2 and your program will terminate because 2 != 'n'. You will need to either input the data correctly as your format expects or clear cin using something like cin.ignore('\n') which will ignore the current line of input.
Further, you start your i at 1 and go up to count but your array is only size 100. If count is 100, it will try to access teikum[100] which will be out of bounds as your array goes from 0 - 99

Inputing Text with spaces closes the program instantly [duplicate]

This question already has answers here:
c++ char array input - explanation
(2 answers)
Closed 5 years ago.
When inputing the text like "School" it reads and outputs how many vowels are in the text but whenever inputing "School School" it instantly closes when pressing enter.
Been trying to see the problem for multiple lessons and cant seem to find it.
#pragma hdrstop
#pragma argsused
#include <tchar.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <iostream.h>
#include <iomanip>
// Autors : Deivids Roberts Saulite
/* Masīvs no N elemmntiem satur anglu alfabēta burtus. Noteikt cik masiva
ir patskanu, kas atrodas masīva elementos ar para numuriem */
int main() {
char teikums[150]; // Masiva lielums
int i, blank, patsk;
char yesno;
do {
system("cls");
patsk = blank = 0;
cout << "Ievadi teikumu:";
cin >> teikums;
cout << "Izmantotie Patskani:";
for (i = 0; teikums[i] != '\0'; i++) {
if (teikums[i] == 'a' || teikums[i] == 'e' || teikums[i] == 'o' ||
teikums[i] == 'o' || teikums[i] == 'u' || teikums[i] == 'A' ||
teikums[i] == 'E' || teikums[i] == 'I' || teikums[i] == 'O' ||
teikums[i] == 'U') {
++patsk;
cout << " " << teikums[i];
}
}
cout << "\nPatskanu Skaits: " << patsk;
cout << ("\nVai velaties atkartot(Y/cits):");
// prasa lietotajam vai velas atkartot
cin >> yesno;
if (yesno == 'y' || yesno == 'Y') {
}
else {
return 0;
}
}
while (tolower(yesno) != 'n');
getch();
}
The second time you read in input, you check if it's a 'y' and if not, you return out of the subroutine. Which since it's main, exits your program.