Here, When I input CO2, it is processing the 'else' statement and if I input anything else it is still the same
I tried changing 'co2' to "co2" but then it doesn't even work
int main(int nNumberofArgs, char* pszArgs)
{
char symb[5];
cout << "Enter Symbol: ";
cin >> symb[5];
if (symb[5] == 'co2')
{
cout << "This is Carbon-Dioxide" << endl;
}
else
{
cout << "Error" << endl;
}
return 0;
}
Your code is written all wrong.
The statement char symb[5]; declares a fixed sized array that can hold 5 char elements max. But when you do cin >> symb[5];, you are not reading up to 5 chars into the array, you are reading a single char into the 6th slot of the array, corrupting surrounding memory.
Also, symb[5] == 'co2' is not the right way to compare the contents of the array. You are comparing the 6th (invalid) char against a single multi-byte character, you are not comparing the whole content of the array against a multi-character string.
Try something more like this instead:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char symb[5];
cout << "Enter Symbol: ";
cin.get(symb, 5);
if (strcmp(symb, "co2") == 0)
{
cout << "This is Carbon-Dioxide" << endl;
}
else
{
cout << "Error" << endl;
}
return 0;
}
That being said, using a std::string instead of a char[] is better:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string symb;
cout << "Enter Symbol: ";
cin >> symb;
if (symb == "co2")
{
cout << "This is Carbon-Dioxide" << endl;
}
else
{
cout << "Error" << endl;
}
return 0;
}
Related
I have a file I want to continue calling on in different functions in my program. It worked fine as a reference in the shiftText function but when I repeated the reference in the next function, all that returns is 0,
Can I get a small hint at something I am missing perhaps to make it behave this way? Thanks!
(PS there's definitely a lot of 'fat' in this that I have included for testing purposes only)
I will eventually return the value of "e" into the shiftText function if you were curious why that's there :)
#include <iostream>
#include <cmath>
#include <fstream>
#include <cctype>
#include <string>
using namespace std;
void inputfile(ifstream &masterfile) // ask for file name
{
string filename;
cout << "Please enter the name and extension of your file " << endl;
cin >> filename;
masterfile.open(filename);
if (!masterfile)
{
cout << "warning: cannot open file" << endl;
exit(1);
}
}
int findShift(ifstream &file, int counter[]) // find most used char
{
char ch;
int code;
while (file.get(ch))
{
code = static_cast<int>(ch);
cout << code << " ";
counter[code]++;
}
int max, min;
int indexMax, indexMin;
max = counter[65];
indexMax = 65;
min = counter[65];
indexMin = 65;
for (int i = 66; i <= 122; i++)
{
if (counter[i] > max)
{
max = counter[i];
indexMax = i;
}
if (counter[i] < min)
{
min = counter[i];
indexMin = i;
}
}
cout << endl
<< "Most frequent was " << indexMax;
return indexMax;
}
void shiftText(ifstream &file) // this is where my program skips over my ifstream reference
{
char ch;
int code;
while (file.get(ch))
{
code = static_cast<int>(ch);
cout << code << " ";
}
}
char stopFlashing() // for sanity
{
char reply;
cout << endl
<< "Press q (or any other key) followed by 'Enter' to quit: ";
cin >> reply;
return 0;
}
int main()
{
int counter[256] = {0};
ifstream file;
inputfile(file);
int e = findShift(file, counter);
shiftText(file);
cout << endl << " " << file << " " << endl; // for testing, a zero is returned
stopFlashing();
}
In function findShift you loop over the file. In function shiftText you are trying to do the same. However, the file is already at its end. Before calling shiftText you should rewind the file by using seekg:
file.seekg(0, std::ios_base::beg)
I want to request the a username and password from 6 users,
and then save this username and password into a text file on my desktop.
I built a structure that has one variable to store the password and a char array with a length of 25.
struct users {
int password;
char username[25];
}
When I want to request the username in the main() function, I use the cin.get() function.
cin.get(username,25);
The program stops after the second cin function when it is storing the password. Why is that?
#include <iostream>
#include <fstream>
struct users {
char username[25];
int password;
} user[6];
using namespace std;
int main() {
cout << "Sign up x6" << endl;
for(int i=0;i<6;i++){
cout << "Username:";
cin.get(user[i].username,20);
cout << "Password:";
cin >> user[i].password;
}
std::ofstream file;
file.open("C:/Users/Programmer/Desktop/sa.txt",ios::app);
for(int i=0;i<6;i++){
file << "first user\n" << user[i].username << endl << user[i].password << endl;
cout << "\n \n \n";
}
}
i found solution that using "gets" function .. which inside stdio.h library:
function "gets" will request the user of value then store the value of variable. (username)
char username[25];
gets(username[25]);
but this function "gets" get only value of "char" type variables so i changed "password" variable type to char
char password[25];
gets(password[25]);
#include<iostream>
#include <fstream>
#include<stdio.h>
struct users
{
char username[25];
char password[25];
} user[6];
using namespace std;
int main()
{
cout << "Sign up x6" << endl;
for (int i = 0; i < 6; i++)
{
cout << "username:";
gets(user[i].username);
cout << "Password:";
gets(user[i].password);
}
std::ofstream file;
file.open("C:/Users/Programmer/Desktop/sa.txt", ios::app);
for (int i = 0; i < 6; i++)
{
file <<"("<<i<<") user\n" << user[i].username << endl << user[i].password<< endl;
cout << "\n \n \n";
}
}
use std::cin.getline(user[i].username,20) instead.
I cannot figure out why my getchar() function is not working the way I want it to work. I am getting 10 not 2. Please take a look.
Main():
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int var, newvar;
cout << "enter a number:" << endl;
cin >> var;
newvar = getchar();
cout << newvar;
return 0;
}
Here is my output:
enter a number:
220
10
Ultimately though I need to be able to distinguish between a '+' '-' or letter or number.
This is maybe not the cleanest way to do it but you can get every char one by one :
#include <iostream>
using namespace std;
int main()
{
int var;
cout << "enter a number:" << endl;
cin >> var;
std::string str = to_string(var);
for(int i=0; i < str.length();++i)
cout << str.c_str()[i] << endl;
return 0;
}
If you enter for example: "250e5" it will get only 250 and skip the last 5.
Edit:
This is just a simple parser and does not do any logic.
If you want to make a calculator I would recommend you to look at what Stroustrup did in his book the c++ programming language.
int main()
{
string str;
cout << "enter a number:" << endl;
cin >> str;
for(int i=0; i < str.length();++i) {
char c = str.c_str()[i];
if(c >= '0' && c <= '9') {
int number = c - '0';
cout << number << endl;
}
else if(c == '+') {
// do what you want with +
cout << "got a +" << endl;
} else if(c == '-')
{
// do what you want with -
cout << "got a -" << endl;
}
}
return 0;
}
I need to write a program that checks if the user-provided first and last names are correctly typed. The program needs to validate that only the first letter of each name part is uppercase.
I managed to write code that checks the first character of the input. So I have a problem when for example "JOHN" is entered.
A correct input would be for example "John Smith".
Here's the code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
std::string str;
cout << "Type First Name: ";
cin >> str;
if(isupper(str[0]))
{
cout << "Correct!" <<endl;
}
else
{
cout << "Incorrect!" <<endl;
}
system("pause");
return 0;
}
The simplest thing you can do is to use a for/while loop. A loop will basically repeat the same instruction for a number of n steps or until a certain condition is matched.
The solution provided is pretty dummy, if you want to read the first name and last name at the same time you will have to spit the string via " " delimiter. You can achieve this result using strtok() in C/C++ or with the help of find in C++. You can see some examples of how to split here.
You can easily modify your code to look like this:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
std::string str;
std::vector<std::string> data = { "First", "Last" };
int j;
for (int i = 0; i < 2; i++) {
cout << "Type " << data[i] << " Name: ";
cin >> str;
if (isupper(str[0])) {
for (j = 1; j < str.size(); j++) {
if (!islower(str[j]))
{
cout << "InCorrect!" << endl;
break; // Exit the loow
}
}
if(j==str.size())
cout << "Correct!" << endl;
}
else {
cout << "InCorrect!" << endl;
}
}
system("pause");
return 0;
}
I am new to C++ and something wrong is happening,
Basically, I have declared a variable called number which is of type int.
If I input a string such as a or x... then number becomes 0. I don't want number to become 0 and instead want it to be error handled.
How do I prevent this is C++? This is my source code...
#include <iostream>
using namespace std;
int number;
int main() {
cout << "Please input a number: ";
cin >> number;
cout << number << endl;
return 0;
}
You need to check what happened in cin:
if (cin >> number) {
cout << number << endl;
}
else {
cout << "error: I wanted a number." << endl;
}
For this you can store the value in a temporary string and then do some conversions to int and double:
#include <iostream>
#include <string>
#include <stdlib.h> //needed for strtod
using namespace std;
int main() {
string str;
cin >> str; //Store input in string
char* ptr; //This will be set to the next character in str after numerical value
double number = strtod(str.c_str(), &ptr); //Call the c function to convert the string to a double
if (*ptr != '\0') { //If the next character after number isn't equal to the end of the string it is not a valid number
//is not a valid number
cout << "It is not a valid number" << endl;
} else {
int integer = atoi(str.c_str());
if (number == (double)integer) { //if the conversion to double is the same as the conversion to int, there is not decimal part
cout << number << endl;
} else {
//Is a floating point
cout << "It is a double or floating point value" << endl;
}
}
return 0;
}
Please note that global variables are bad, write them inside a scope (a function or a class, for example)