Be a Supporter!

C++ Problemo

  • 731 Views
  • 12 Replies
New Topic Respond to this Topic
Cinjection
Cinjection
  • Member since: Apr. 6, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
C++ Problemo 2005-05-20 18:30:39 Reply

Ok well i have this small problem. I delcare a file type with this line:
char filetype[255];
The I get a user input with the following line:
cin>>filetype;
Nothing new. Now here's the problem. I run filetype through this:
do
if((filetype != "vb")||(filetype != "cpp")){
error = true;
cout<<"...invalid file type!"<<endl;
cout<<"Visual Basic = vb"<<endl;
cout<<"C/C++ = cpp"<<endl;
cin>>filetype;
}
else
error = false;
while (error = true);

But no matter what i enter i get the ...invalid error message. I tryed couting out the result and i get whatever i entered, but what i look at the var in the watch, i get 0xfe23d [value i entered]. So i guess the first thing is the var addres or something. Anyone knows how to fix it?(sinnernaut don't say "iostream is the devil"....lol);P

thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++ Problemo 2005-05-20 18:34:30 Reply

iostream does suck. Admit it. Also, instead of char filetype[255]; it should be string filetype;
Also be sure to #include <string>

Then that code will work fine. There seems to be a misconception here about C++ style strings and C style strings.

There are some important differences:
With C style, you have to do if(strcmp(asdf,"varvalue") == 0)
With C++ style, you can do if(asdf == "varvalue");

Also, with C style you have to declare size so it knows how huge of a buffer to push onto The Stack, while with C++ style, the string class dynamically allocates memory for you and puts that shit on The Heap.


omg.
Playstation Network tag: muffin-noodle
the empty set

Cinjection
Cinjection
  • Member since: Apr. 6, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to C++ Problemo 2005-05-20 18:38:35 Reply

When i include <string> i get "string is an undeclared identifier whae i try to declare filetype. Any ideas?

Mo4545
Mo4545
  • Member since: Jan. 6, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++ Problemo 2005-05-20 22:28:05 Reply

Try string.h

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to C++ Problemo 2005-05-20 22:34:42 Reply

#incluede <iostream>
#include<string.h>
string filetype;
cin>>filetype;
do
if((filetype != "vb")||(filetype != "cpp")){
error = true;
cout<<"...invalid file type! You entered ("<<filetype;<<")"<<endl;
cout<<"Visual Basic = vb"<<endl;
cout<<"C/C++ = cpp"<<endl;
cin>>filetype;
system("pause"); // Pause just in case
}
else
error = false;
while (error = true);

Try that, I'm not sure if it'll work, I just wrote it spur of the moment.

Mo4545
Mo4545
  • Member since: Jan. 6, 2005
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++ Problemo 2005-05-20 22:37:14 Reply

Post your full code cinjection, so we can see whats goin on. We`ll be of much more help if we saw everything.

thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++ Problemo 2005-05-20 22:38:30 Reply

At 5/20/05 10:34 PM, Pilot-Doofy wrote: code

Brilliant job on leaving out main(); and using whorey system(); functions.

:)


omg.
Playstation Network tag: muffin-noodle
the empty set

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to C++ Problemo 2005-05-20 22:41:48 Reply

At 5/20/05 10:38 PM, Sinnernaut wrote: Brilliant job on leaving out main(); and using whorey system(); functions.

Sorry, I'm a bit high and c++ isn't my favorite to begin with. Lawl. ASK ME A PHP QUESTION YOU FUCKS!!!

Ravens-Grin
Ravens-Grin
  • Member since: Jun. 3, 2003
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to C++ Problemo 2005-05-20 23:28:59 Reply

add a { after the do command and a } before the while command. That might be your problem.

basic template

do
{
// Add commands here
// Yep...
} while ( statement );

ManateeGod
ManateeGod
  • Member since: May. 26, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to C++ Problemo 2005-05-21 00:30:58 Reply

At 5/20/05 06:30 PM, Cinjection wrote: Ok well i have this small problem. I delcare a file type with this line:
char filetype[255];

In C++, you don't need to declar char arrays, use the string header.

The I get a user input with the following line:
cin>>filetype;
Nothing new. Now here's the problem. I run filetype through this:

Don't you need cin.get() afterwards?

do
if((filetype != "vb")||(filetype != "cpp")){
error = true;
cout<<"...invalid file type!"<<endl;
cout<<"Visual Basic = vb"<<endl;
cout<<"C/C++ = cpp"<<endl;
cin>>filetype;
}
else
error = false;
while (error = true);

Also, this might be a typo, but what you want here is error == true, no?


But no matter what i enter i get the ...invalid error message. I tryed couting out the result and i get whatever i entered, but what i look at the var in the watch, i get 0xfe23d [value i entered]. So i guess the first thing is the var addres or something. Anyone knows how to fix it?(sinnernaut don't say "iostream is the devil"....lol);P

You're missing the main declaration and all the other stuff, so I don't know if there might be a problem somewhere else. Iostream works perfectly fine, though.

thoughtpolice
thoughtpolice
  • Member since: Mar. 24, 2003
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++ Problemo 2005-05-21 00:41:18 Reply

At 5/21/05 12:30 AM, ManateeGod-Sama wrote: Don't you need cin.get() afterwards?

No, you must be refering to cin.ignore(); which is the PERL equivalent of chomp(); and strips newline (I believe. I. Hate. IOStream. It's ridiculously bloated, and all the functions look like complete shit.

Really. They do.)

And if he WAS using cin.get(); he'd have to do fflush(stdin); beforehand so cin doesn't read shit clogging the input buffer.


omg.
Playstation Network tag: muffin-noodle
the empty set

CronoMan
CronoMan
  • Member since: Jul. 19, 2004
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to C++ Problemo 2005-05-21 06:22:07 Reply

filetype = "";
error = true;

do
{
if( ( !strcmp(filetype, "vb") ) || ( !strcmp(filetype, "cpp") ) )
{
cout<<"...invalid file type!"<<endl;
cout<<"Visual Basic = vb"<<endl;
cout<<"C/C++ = cpp"<<endl;
cin>>filetype;
}
else
error = false;

}
while (error == true);

try this.

you need {} on a do-while loop, and in the parameter in while, you were missing one =, so that would return true no matter what.
And you can use comparison operators on strings (char * is just an address, so you would be comparing addresses, use strcmp (returns 0 if the strings are identical))


"no sound in ass"

Cinjection
Cinjection
  • Member since: Apr. 6, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to C++ Problemo 2005-05-21 09:41:57 Reply

Ok well i sprta figured it out. I forgot using namespace std; and string requires that. Then i learned that cin>> doesn't work with a string for some reason (error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion))

So i used the C counterpart: scanf("%s", filepath); (i think thats what it is). When i run the program then and enter a input i get an acess violation and it closes, heres the current code:

//Prorammed by Oleksi Derkatch/ Cinjection
//May 19 2005
//counts number of lines in programming projects.

#include <iostream.h>
#include <string>
#include <stdio.h>

using namespace std;

int main(){

int lines = 0;
string filetype;
string filepath;
bool error;

cout<<"Welcome to SPS Line Counter!"<<endl;
cout<<"Please enter the programming language."<<endl;
cout<<"Visual Basic = vb"<<endl;
cout<<"C/C++ = cpp"<<endl;
scanf("%s", filepath);

//exeption handling
do{
if((filetype != "vb")||(filetype != "cpp")){
error = true;
cout<<"...invalid file type!"<<endl;
cout<<"Visual Basic = vb"<<endl;
cout<<"C/C++ = cpp"<<endl;
scanf("%s", filepath);
}
else
error = false;
}while (error = true);
//end exeption handling

cin.get();
return 0;
}