I'm a little bit confused with that, but I cant understand why one code is working and another isn't.. Seems like it's the same, but largest one cathes and error in the string with calling function "GetFileAttributes".
The first code is not working.
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
if (argc < 2){
char answer;
do
{
cout << "You didn't type in the path to the file. Do you want to do it now? [y/n]" << endl;
cin >> answer;
} while (!cin.fail() && answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N');
cin.clear();
if (answer == 'N' || answer == 'n'){
return 0;
}
printf("%s", "Type in the path to the file\n");
scanf("%s", (argv + 1));
}
//LPCWSTR l = (LPCWSTR) (argv+1);
DWORD d = GetFileAttributes(argv[1]);
printf("%s", "Attrbiutes for this file are\n");
if (d == INVALID_FILE_ATTRIBUTES){
printf("%s", "Invalid attributes\n");
}
if (d & FILE_ATTRIBUTE_ARCHIVE){
printf("%s", "Archive\n");
}
if (d & FILE_ATTRIBUTE_COMPRESSED){
printf("%s", "Compressed\n");
}
if (d & FILE_ATTRIBUTE_DIRECTORY){
printf("%s", "Directory\n");
}
if (d & FILE_ATTRIBUTE_HIDDEN){
printf("%s", "Hidden\n");
}
if (d & FILE_ATTRIBUTE_READONLY){
printf("%s", "Read-only\n");
}
_getch();
return 0;
}
Here is the working one
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
DWORD x = GetFileAttributes(argv[1]);
if (x == INVALID_FILE_ATTRIBUTES)
{
std::cout << "error" << std::endl;
return 0;
}
if (x & FILE_ATTRIBUTE_ARCHIVE)
{
std::cout << "archive" << std::endl;
}
if (x & FILE_ATTRIBUTE_HIDDEN)
{
std::cout << "hidden" << std::endl;
}
if (x & FILE_ATTRIBUTE_READONLY)
{
std::cout << "read only" << std::endl;
}
if (x & FILE_ATTRIBUTE_SYSTEM)
{
std::cout << "system" << std::endl;
}
if (x & FILE_ATTRIBUTE_TEMPORARY)
{
std::cout << "temporary" << std::endl;
}
_getch();
return 0;
}
This is not the right way to use argv. DO NOT MODIFY IT. Treat it as read-only. Use a separate variable to hold the filename, prompting the user to populate that variable if needed, and then use the variable instead of argv when using the filename.
Also, new OS version introduce new file attributes, which you are not taking into account. You should output all attributes, even ones you do not recognize.
Also, you should stop mixing C and C++ I/O. Pick one or the other and be consistent with it.
Try something more like this:
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
std::string filename;
char ch;
if (argc >= 2)
{
filename = argv[1];
}
else
{
do
{
std::cout << "You didn't provide a path to the file. Do you want to type it now? [y/n]" << std::endl;
if (!(std::cin >> ch))
return 0;
}
while ((ch != 'y') && (ch != 'Y') && (ch != 'n') && (ch != 'N'));
if ((ch == 'N') || (ch == 'n'))
return 0;
cin.clear();
do
{
std::cout << "Type in the path to the file" << std::endl;
if (!std::getline(std::cin, filename))
return 0;
}
while (filename.empty());
}
DWORD d = GetFileAttributesA(filename.c_str());
std::cout << "Attributes for file: " << filename << std::endl;
if (d == INVALID_FILE_ATTRIBUTES)
{
std::cout << "Invalid attributes" << std::endl;
}
else
{
if (d & FILE_ATTRIBUTE_ARCHIVE)
{
std::cout << "Archive" << std::endl;
d &= ~FILE_ATTRIBUTE_ARCHIVE;
}
if (d & FILE_ATTRIBUTE_COMPRESSED)
{
std::cout << "Compressed" << std::endl;
d &= ~FILE_ATTRIBUTE_COMPRESSED;
}
if (d & FILE_ATTRIBUTE_DIRECTORY)
{
std::cout << "Directory" << std::endl;
d &= ~FILE_ATTRIBUTE_DIRECTORY;
}
if (d & FILE_ATTRIBUTE_HIDDEN)
{
std::cout << "Hidden" << std::endl;
d &= ~FILE_ATTRIBUTE_HIDDEN;
}
if (d & FILE_ATTRIBUTE_READONLY)
{
std::cout << "Read-only" << std::endl;
d &= ~FILE_ATTRIBUTE_READONLY;
}
if (d != 0)
{
std::cout << "Other: " << std::hex << std::showbase << std::setw(8) << std::setfill('0') << d << std::endl;
}
}
std::cout << "Press a key to exit" << std::endl;
std::cin >> ch;
return 0;
}
Related
I got a function but i can't define any variable inside and global too. This function gets a char value from user. I need to define this value to main function. How can i do it? Thanks for helping guys.
This is my code. I made it like this but I define variables in global but I need to define this variables in only main.
#include <iostream>
using namespace std;
char cName[255], cSurname[255];
bool nameFunc() {
cout << "Whats Your Name ?\n";
cin >> cName;
if (cName != NULL && cName[0] == '\0') {
return false;
}
else {
return true;
}
}
bool surnameFunc() {
cout << "Whats Your Surname ?\n";
cin >> cSurname;
if (cSurname != NULL && cSurname[0] == '\0') {
return false;
}
else {
return true;
}
}
int main() {
if (nameFunc() and surnameFunc()) {
cout << "Hello, " << cName << " " << cSurname << "." << endl;
}
else {
cout << "Error! Name or Surname is empty." << endl;
}
system("PAUSE");
return 0;
}
You could pass references to your variables to the functions. Since the char[] have a fixed length, you need to make sure that you don't write out-of-bounds which complicates things.
Example:
#include <iostream>
template<size_t N>
bool nameFunc(char (&cName)[N]) {
std::cout << "Whats Your Name ?\n";
std::cin.getline(cName, N); // read at most N chars
return cName[0] != '\0';
}
template<size_t N>
bool surnameFunc(char (&cSurname)[N]) {
std::cout << "Whats Your Surname ?\n";
std::cin.getline(cSurname, N); // read at most N chars
return cSurname[0] != '\0';
}
int main() {
char cName[255], cSurname[255];
if (nameFunc(cName) and surnameFunc(cSurname)) {
std::cout << "Hello, " << cName << " " << cSurname << ".\n";
}
else {
std::cout << "Error! Name or Surname is empty.\n";
}
}
A much easier option would be to use std::strings and pass them by reference to the functions.
Is it what you're looking for?
#include <iostream>
using namespace std;
bool nameFunc(char* cName) {
cout << "Whats Your Name ?\n";
cin >> cName;
if (cName != NULL && cName[0] == '\0') {
return false;
}
else {
return true;
}
}
bool surnameFunc(char* cSurname) {
cout << "Whats Your Surname ?\n";
cin >> cSurname;
if (cSurname != NULL && cSurname[0] == '\0') {
return false;
}
else {
return true;
}
}
int main() {
char cName[255], cSurname[255];
if (nameFunc(cName) and surnameFunc(cSurname)) {
cout << "Hello, " << cName << " " << cSurname << "." << endl;
}
else {
cout << "Error! Name or Surname is empty." << endl;
}
system("PAUSE");
return 0;
}
This question already has answers here:
How to determine if a string is a number with C++?
(36 answers)
Closed 4 years ago.
I'm trying to enter the format of
"prog1 1 all file1"
the first input has to be a argv[1] should be an int.
So i need a way to determine if argv[1] is entered as a string "xxx" ( "prog1 xxx" ) it should return "NO PHRASE LENGTH" but its returning "INVALID PHRASE LENGTH".
I see there is a isdigit() function but im not sure how i would use that.
int main(int argc, char *argv[]){
try{
if(argc == 1){
cout << "NO PHRASE LENGTH" << endl; exit(1);
}
else if((stoi(argv[1])) <= 0 ){
cout << "INVALID PHRASE LENGTH" << endl; exit(1);
}
else if(argc == 2){
cout << "NO MODE" << endl; exit(1);
}
else if(!(std::string(argv[2]) == "all") && !(std::string(argv[2]) == "top")){
cout << "INVALID MODE" << endl;
}
else if(argc == 3){
cout << "NO FILES GIVEN" << endl;
}
else if(argc >= 4){
ifstream f;
for(int i = 4; i < argc; i--){
f.open( argv[i] );
if( ! f.good() ) {
cout << "BAD FILE " << argv[i] << endl; exit(1);
}
//cout << "OK" << endl;
//f.close();
}
}
else
return 0;
}
catch(exception e){
}}
Your code is quite confusing. I am not sure what you were doing with the for loop to open the file. Anyway, I have given an example of what it could be.
int main(int argc, char *argv[])
{
if(argc != 4)
{
cout << "Program requires 3 parameters!" << endl;
return -1;
}
if(std::string(argv[2]) != "all" && std::string(argv[2]) != "top")
{
cout << "INVALID MODE" << endl;
return -1;
}
try
{
if(stoi(argv[1]) < 1)
{
cout << "ZERO OR NEGATIVE PHRASE LENGTH" << endl;
return -1;
}
ifstream f(argv[3]);
if(!f)
{
cout << "BAD FILE " << argv[3] << endl;
return -1;
}
// Now do whatever you want with the opened file
}
catch(out_of_range e)
{
cout << "NON-INT PHRASE LENGTH" << endl;
return -1;
}
return 0;
}
Don't use std::endl when all you want to say is '\n' (or "...\n"). std::endl not only inserts a newline into the stream but also flushes it. If you *really* want to flush a stream be explicit and use std::flush.
Don't use exit() to end a program if there are other ways to do so. If you use exit() no stack unwinding will occur.
#include <cstdlib>
#include <string>
#include <iostream>
#include <exception>
#include <fstream>
int main(int argc, char **argv)
{
if (argc == 1) {
std::cerr << "NO PHRASE LENGTH\n";
return EXIT_FAILURE;
}
if (argc == 2) {
std::cerr << "NO MODE\n";
return EXIT_FAILURE;
}
if (argc == 3) {
std::cerr << "NO FILES GIVEN\n";
return EXIT_FAILURE;
}
int phrase_length;
try {
phrase_length = std::stoi(argv[1]);
if (phrase_length < 0)
throw std::out_of_range{ nullptr };
}
catch (std::invalid_argument) {
std::cerr << "NO PHRASE LENGTH";
return EXIT_FAILURE;
}
catch (std::out_of_range) {
std::cerr << "PHRASE LENGTH OUT OF RANGE\n";
return EXIT_FAILURE;
}
std::string mode{ argv[2] };
if ( mode != "all" && mode != "top") {
std::cerr << "INVALID MODE\n";
return EXIT_FAILURE;
}
for (int i = 3; i < argc; ++i) {
std::fstream input_file{ argv[i] };
if (!input_file.is_open()) {
std::cerr << "BAD FILE \"" << argv[i] << "\"\n";
return EXIT_FAILURE;
}
}
}
I am writing a simple bracket checker. Should be pretty easy. I had it working when it was all in one function, but I am required to also make something for stdin. So I thought it was best to make 2 functions. That being said I am getting an error on the checking if the stack is null on line 82. for whatever reason it is not allowing me to check if the top of my stack is null. I tried in a testing program to see if it was some sort of referencing error or if it was going out of scope by going into the other method. Its not. Should work fine because it is a global variable.
Thoughts on what I am doing wrong? All my interneting and knowledge points me to the idea that I am doing it correctly.
Below is all of the code. its compilable. if I need to clarify anything I would be more than happy to.
Thanks
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <fstream>
#include <cstdlib>
using namespace std;
stack<char> BracketsCheck;
int linecounter = 0;
int FileNumber = 1;
int pos;
string str ="";
string filename;
int validate(string string)
{
int size = str.size();
for (int i = 0; i < str.size(); i++)
{
pos = i;
if ((str[i] == '(' ) || (str[i] == '[') || (str[i] == '{'))
{
BracketsCheck.push(str[i]);
}
else if (str[i] == ')')
{
if (BracketsCheck.top() == '(')
BracketsCheck.pop();
else
{
cout << filename << ":" << linecounter << ":" << pos << "ERROR: missing open parenthesis" << endl;
return EXIT_FAILURE;
}
}
else if (str[i] == ']')
{
if (BracketsCheck.top() == '[')
BracketsCheck.pop();
else
{
cout << filename << ":" << linecounter << ":" << pos << "ERROR: missing open squre bracket" << endl;
return EXIT_FAILURE;
}
}
else if (str[i] == '}')
{
if (BracketsCheck.top() == '{')
BracketsCheck.pop();
else
{
cout << filename << ":" << linecounter << ":" << pos << "ERROR: missing open curly brace" << endl;
return EXIT_FAILURE;
}
}
}
}
int main(int argc, char* argv[])
{
// BracketsCheck.top() = 'h';
if (argc == 1)
{
cin >> str;
cout << "no arguments" << endl;
validate (str);
return 0;
}
else
{
while (argv[FileNumber] != NULL)
{
filename = argv[FileNumber];
ifstream inFile(argv[FileNumber]);
cout << argv[FileNumber]<<endl;
while (getline(inFile, str))
{
validate(str);
linecounter++;
}
if (BracketsCheck.top() != NULL)
{
cout << "got to null checker" << endl;
cout << filename << ":" << linecounter << ":" << pos << "umatched closing brace" << endl;
return EXIT_FAILURE;
}
FileNumber++;
}
return 0;
}
}
Based on your response to my comments. If you are trying to check if the stack is not empty you should use !BracketsCheck.empty() also:
int validate(string string)
is probably not a good idea since you will hiding the string type.
top() will return a reference or const reference not a pointer and if your stack is empty you should not be calling top.
I would also discourage you from using:
using namespace std;
it is considered bad practice I realize typing std:: all the time can be annoying at first but you really do get used to it after a while.
Finally validate needs a return statement since it is supposed to return int and flowing off the end of function without a return in this case will invoke undefined behavior as per 6.6.3 The return statement paragraph 2 from the draft C++ standard.
I was trying to count the number of characters in a string class but for some reason the program is skipping over my function completely. This is just the test code from the main program, it still was giving me the same results. How come the counter function is skipped over?
#include <iostream>
#include <string>
using namespace std;
void prompt(string& dna)
{
cout << "Input: ";
getline(cin, dna);
}
void counter(const string DNA,
int* a_count, int* t_count, int* c_count, int* g_count)
{
for (int i = 0; i < DNA.size(); i++)
{
if (DNA.at(i) == 'a')
{
*a_count++;
}
else if (DNA.at(i) == 't')
{
*t_count++;
}
else if (DNA.at(i) == 'c')
{
*c_count++;
}
else if (DNA.at(i) == 'g')
{
*g_count++;
}
}
}
int main()
{
string dna;
int a = 0;
int t = 0;
int c = 0;
int g = 0;
prompt(dna);
if (! dna.empty())
{
cout << "Before:\n"
<< "A: " << a << endl
<< "T: " << t << endl
<< "C: " << c << endl
<< "G: " << g << endl;
counter(dna, &a, &t, &c, &g);
cout << "\n\nAfter:\n"
<< "A: " << a << endl
<< "T: " << t << endl
<< "C: " << c << endl
<< "G: " << g << endl;
}
system("pause");
return 0;
}
You're applying operator ++ the wrong way. It should be:
if (DNA.at(i) == 'a')
{
(*a_count)++;
}
else if (DNA.at(i) == 't')
{
(*t_count)++;
}
else if (DNA.at(i) == 'c')
{
(*c_count)++;
}
else if (DNA.at(i) == 'g')
{
(*g_count)++;
}
You've got a priority problem between the ++ and * operators. You are incrementing the pointer address, not the value. (*a_count)++; would be correct.
You may find it easier to use reference parameters for the counts instead, since you don't actually need to do any pointer arithetic. ie:
void counter(const string DNA, int& a_count, int& t_count, int& c_count, int& g_count)
And, yes a switch statement would be neater.
Ok, after failing to read a polynomial, I'm trying first a basic approach to this.
So i have class polinom with function read and print:
#ifndef _polinom_h
#define _polinom_h
#include <iostream>
#include <list>
#include <cstdlib>
#include <conio.h>
using namespace std;
class polinom
{
class term
{
public:
double coef;
int pow;
term(){
coef = 0;
pow = 0;
}
};
list<term> poly;
list<term>::iterator i;
public:
void read(int id)
{
term t;
double coef = 1;
int pow = 0;
int nr_term = 1;
cout << "P" << id << ":\n";
while (coef != 0) {
cout << "Term" << nr_term << ": ";
cout << "coef = ";
cin >> coef;
if (coef == 0) break;
cout << " grade = ";
cin >> pow;
t.coef = coef;
t.pow = pow;
if (t.coef != 0) poly.push_back(t);
nr_term++;
}
}
void print(char var)
{
for (i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them
if (poly.size() < 2) {
if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient
cout << i->coef;
else if (i->pow == 1) {
if (i->coef == 1)
cout << var;
else if (i->coef == -1)
cout << "-" << var;
else
cout << i->coef << var;
}
else
cout << i->coef << var << "^" << i->pow; //otherwise we print both
}
else {
if (i == poly.end()) { // if we reached the last term
if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient
cout << i->coef;
else if (i->pow == 1)
cout << i->coef << var;
else
cout << i->coef << var << "^" << i->pow; //otherwise we print both
}
else {
if (i->coef > 0) {
if (i->pow == 1)//if the coef value is positive
cout << i->coef << var << " + "; //we also add the '+' sign
else
cout << cout << i->coef << var << "^" << i->pow << " + ";
}
else {
if (i->pow == 1)//if the coef value is positive
cout << i->coef << var << " + "; //we also add the '+' sign
else
cout << cout << i->coef << var << "^" << i->pow << " + ";
}
}
}
}
}
};
#endif
Well, it works when reading only one term but when reading more the printed coefficients are some random values and also after the last term it print '+' or '-' when it shouldn't.
So any idea what's wrong?
Thanks!
FINAL UPDATE
Ok, i made it work perfectly by modifying Bill's code so thanks a lot Bill and everyone else who commented or answered!
Here's the final print function:
void print(char var)
{
list<term>::iterator endCheckIter;
for (i=poly.begin() ; i != poly.end(); i++ )
{
//going through the entire list to retrieve the terms and print them
endCheckIter = i;
++endCheckIter;
if (i->pow == 0)
cout << i->coef;
else if (i->pow == 1)
cout << i->coef << var;
else
cout << i->coef << var << "^" << i->pow;
if (endCheckIter != poly.end()) {
if (endCheckIter->coef > 0)
cout << " + ";
else {
cout << " - ";
endCheckIter->coef *= -1;
}
}
}
}
if (i == poly.end()) { // if we reached the last term
This comment shows your error. For any given collection of items, items.end() returns the entry after the last item.
For instance, say I have a 5-item std::vector:
[0] [1] [2] [3] [4]
Then begin() points to:
[0] [1] [2] [3] [4]
/\
And end() points to:
[0] [1] [2] [3] [4] []
/\
Your for loop, it looks like:
for (i=poly.begin() ; i != poly.end(); i++ )
Note that comparing i to poly.end() happens before iter is used. As soon as i == poly.end(), you're done.
Your code inside of if (i == poly.end()) { will never be executed because this can never be true.
You can test for the end using the following:
// get access to the advance function
#include <iterator>
....
std::list<term>::iterator endCheckIter = i;
std::advance(endCheckIter, 1);
if (endCheckIter == poly.end())
{
...
}
But a simpler way might be:
std::list<term>::iterator endCheckIter = i;
++endCheckIter;
if (endCheckIter == poly.end())
{
...
}
Edit:
I'm not sure why you're getting garbage. Add in your missing braces and handle the non-end case, and everything works here:
void print(char var)
{
list<term>::iterator endCheckIter;
for (i=poly.begin() ; i != poly.end(); i++ )
{ // <- MISSING BRACE
//going through the entire list to retrieve the terms and print them
endCheckIter = i;
++endCheckIter;
cout << i->coef << var << "^" << i->pow; // <- MISSING OUTPUT
if (endCheckIter != poly.end()) {
if (i->coef > 0)
cout << " + ";
else
cout << " - ";
}
} // <- MISSING BRACE
}
Okay, now that Vlad has decided how he's going to do it, here's how I'd do it:
#ifndef _polinom_h
#define _polinom_h
#include <iostream>
#include <list>
#include <cstdlib>
#include <cmath>
#include "infix_iterator.h"
using namespace std;
char var;
class polinom {
class term {
double coef;
int power;
ostream &write(ostream &os) const {
// At least to me, the logic is easier to follow if we
// handle one piece at a time.
// It may be longer, but I think it's easier to understand.
// First, if the coefficient is negative, subtract the term instead of adding it.
if (coef < 0)
// backspace over the "+ " and print '- ' in its place.
os << "\b\b- ";
// Then print the absolute value of the coefficient (if needed).
if (fabs(coef) != 1)
os << fabs(coef);
// Then print the var (if needed)
if (power != 0)
os << var;
// then print the power (if needed)
if (abs(power) > 1)
os << "^" << power;
// And we're done.
return os;
}
// support inserting a term into a stream.
friend std::ostream &operator<<(std::ostream &os, term const &t) {
return t.write(os);
}
public:
term(double c=0.0, int p=0) : coef(c), power(p) {}
bool read(std::ostream &os, std::istream &is, int num) {
// This is only slightly modified from the originally posted question
os << "\nTerm " << num << ": coef = ";
is >> coef;
if (coef == 0.0)
return false;
if (coef != 0.0) {
os << " grade = ";
is >> power;
}
return true;
}
bool operator<(term const &other) const {
// order by descending powers.
return other.power < power;
}
};
list<term> poly;
public:
void read(int id) {
term t;
int nr_term = 1;
std::cout << "P: " << id;
// Read and save individual terms:
while (t.read(std::cout, std::cin, nr_term++))
poly.push_back(t);
}
void write(char var) {
// sort the polynomial so the highest powers come first.
poly.sort();
// save the variable name for later use.
::var = var;
// Print out all the terms:
std::copy(poly.begin(), poly.end(), infix_ostream_iterator<term>(std::cout, " + "));
}
};
#endif
Using this is pretty trivial:
#include "polynom.h"
int main() {
polinom p;
p.read(1);
p.write('x');
return 0;
}
void print(char var)
{
for (list<term>::const_iterator i = poly.begin(), e = poly.end(); i != e; ++i) {
if (i != poly.begin() || i->coef < 0) {
cout << (i->coef > 0 ? '+' : '-');
}
if (abs(i->coef) != 1) {
cout << abs(i->coef);
}
if (i->pow == 0) {
if (abs(i->coef) == 1) {
cout << 1;
}
} else {
cout << var;
if (i->pow != 1) {
cout << '^' << i->pow;
}
}
}
}