Inputting array problems - c++

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

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;
}

Fixing uninitialized local variable error

I am working on a project right now and when I try to run what I have below it gives me an error that says "uninitialized local variable 'userOption' used" on line 22, while (isValidOption(userOption) == true) {.
How do I fix that error? Thank you.
#include<iostream>
#include <string>
using namespace std;
char toupper(char ch) {
if (ch >= 'A'&&ch <= 'Z')
return(ch);
else
return(ch - 32);
}
bool isValidOption(char ch) {
if (ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X')
return(true);
else
return(false);
}
char getMainOption() {
string UserInput;
char userOption;
while (isValidOption(userOption) == true) {
cout << "Choose One of the following options\n";
cout << "I--List Our Inventory\n";
cout << "O--Make an Order\n";
cout << "L--List all Orders made\n";
cout << "X--Exit\n";
cout << "Enter an option: ";
getline(cin, UserInput);
userOption = toupper(UserInput[0]);
if (!isValidOption(userOption)) {
cout << "Invalid String\n";
cout << "Enter an option: ";
getline(cin, UserInput);
userOption = toupper(UserInput[0]);
}
if (userOption == 'I')
cout << "Listing Our Inventory\n";
else if (userOption == 'O')
cout << "Make an order\n";
else if (userOption == 'L')
cout << "Listing all orders\n";
}
return userOption;
}
int main() {
char choice;
choice = getMainOption();
system("pause");
return 0;
}
What the error is saying that you're trying to read from userOption before you've ever written to it. If a variable is uninitialized, its memory contents will be full of junk left behind by other functions and it can easily cause bugs. In your case, you'll want to read input from the user into userOption before you do any logic on it. This can be done with a do-while loop:
char userOption; // not yet initialized
do {
...
cin >> userOption; // userOption gets initialized here on first loop run
} while (isValidOption(userOption)); // no need for == true, that's a tautology :-)
// NOTE: perhaps you want to loop while the input is INvalid, as in
// while (!isValidOption(userOption)); ?
A couply code-review comments I would additionally give are:
std::toupper already exists in <cctype>. Docs are here
return is not a function call and it's better to write return ch; than return(ch);
if (ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X'){ return true; } else { return false; } is completely equivalent to the shorter return ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X';
Also take a look at system(“pause”); - Why is it wrong?
Happy coding! Let me know if questions remain

Blank answers dynamic array

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.

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.