Hello and Help

This is a discussion on Hello and Help within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I just registered into this forum a few minutes ago. I'm a first year Mechanical Engineering student, fourth ...

  1. #1
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37

    Unhappy Hello and Help

    Hi everyone. I just registered into this forum a few minutes ago.
    I'm a first year Mechanical Engineering student, fourth week in University.

    I have already read the Forum Guidelines and Homework thread. I have also scanned through the FAQ board.
    I'm glad to have found this Forum and register. I know that I will benefit a lot from this forum, for my course. Glad to be here.

    I have just recieved the assignment questions for C++ from my university and I'm having problems. My lecturer on C++ seem to be not so good in explaining what he's trying to teach, so I'm having problems. Also one of the questions in the Assignment uses a method that we haven't learn, even though I think that we will learn that sooner or later, I would want to learn it now. I like to be ahead of my class. I have read through a few tutorial websites, and I also tried reading a book on C++. But I have difficulty understanding certain things because of my n00bness.... ...

    There were three questions and I mannage to complete one of them.
    For one of the other question, I need to know is how to use and if on words instead of integers. I tried reading the up string stuff, but I have difficulty understanding...

    The question requires us to write a prgramme that can tell the realtion and the physical details of my family members. If I use numbers to represent my family integers, I am able to complete it.
    But i don't know how to use words (writing thier name down) to represent them.
    I read this FAQ how do i convert a string to an integer?? (C) but I still don't understand.

    Also, I installed (well, actually my brother installed) a Microsoft Visual C++ 6.0. It works for him, but it doesn't work for me. Cause I learn to use <iostream.h>, but he uses something else, which I don't know. He does Electrical and Electronic Engineering. I know that Win32 isn't available in Projects, under New where as it is available int the University's Computer lab. How do I get it to work for <iostream.h>?..


    Someone please help. Thank you.

    [Z-D]
    Last edited by [Z-D]; 10-19-2006 at 08:54 AM.

  2. #2
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    the thread you went was talking about C... i know only a bit of C++, been using C, tho the syntax is almost the same... but I don't know atoi equivalent for C++

    it should be <iostream> (without the .h) btw.

    as for your question, you might want to try this
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
       char s[100];
    
       cin >> s;
       cout << s << '\n';
    
       return 0;
    }
    if you are looking for a c++ compiler... get cygwin and install the gcc package

    http://www.cygwin.com/

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    atoi equivalent is string streams, but you can still use atoi() or strtol().
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Hey....... Aren't you gonna welcome me?

    Thanks a lot. Could you explain more on what that does. I don't have C++ with me right now, so I can't test it... sorry..

    Also, what I needed to know was how to use if for words.


    Something which works like this (but obviously this doesn't work)
    Code:
    #include <iostream.h>
    
    main ()
    {
    	char name;
    
    	cout << "Enter the name of my family members:";
    	cout << "Family members are '[name1]', '[name2', '[name3]', ...etc ";
    	cin >> name;
    
    	if (name = "name1]);
    	{cout << "Full Name: [bla bla bla]" << endl;
    	cout << "Relation: Father";
    	cout << 'Height, bla bla bla" << endl;
    	}
    
    	if (name = "name2]);
    	{cout << "Full Name: [bla bla bla]" << endl;
    	cout << "Relation: Mother";
    	cout << 'Height, bla bla bla" << endl;
    	}
    
    ....
    ....
    ...
    etc...
    
    
    	else if ( name != [name1], name != [name2], [name3], ...etc );
    	{cout << "the name written is not my family member... ha ha!!!" << endl;
    
    	return 0;
    }
    That's what I meant.... It works on integer but not on characters.... How do I make if work on characters?......

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    To compare null terminated strings, you should use strcmp(). I suggest that you actually work with a compiler, considering the considerable number of errors in your code that you have no easy way (aside from asking us) of checking.

    1. The correct standard header is <iostream>, not <iostream.h>
    2. The names cout, cin, and endl should be fully qualified, unless you have a using directive for them.
    3. In your code, name is a char, not a string.
    4. It appears that you are trying to compare using the assignment operator.
    5. It appears that you are trying to use the comma operator where you actually want a logical and... and in the first place, an else would suffice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Hello and welcome
    6) String literals are between double quotes, not a mix of quotes and square brackets.

  7. #7
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Thank you, but

    Okay then,.... I'll just read up more...

    1. Whats the difference between <iostream> and <iostream.h>? How do we know when t use them? (MAN! My lecturer sucks!!)
    3. Actually I wasn't thaugh how to use char of string yet... I just read up and tried experimenting in the Comp Lab.
    5. sorry,... i don't really understand...
    6. screwed up there....


    To let you have an Idea on how n0000b I am...

    I was happy enough to answere this question.
    Write a programme that can convert temperature from RFarenheit to Celcius and viceversa:
    Code:
    #include <iostream>
    
    main()
    {
    	int ConversionChoice, F, C;
    
    	cout << "Press '1' for F to C, and '2' for C to F:";
    	cin >> ConversionChoice;
    
    	if (ConversionChoice == 1)
    	{cout << "Enter temperature in degree Farenheit:";
    	cin >> F;
    
    	C = (F - 32) / 1.8; 
    
    	cout << "Temperature in Degree Celcius:" << C << endl;
    	}
    
    	if (ConversionChoice == 2)
    	{cout << "Enter temperature in degree Celcius:";
    	cin >> C;
    
    	F = 1.8*C + 32;
    
    	cout << "Temperature in Degree Farenheit:" << F << endl;
    	}
    
    	else if (ConversionChoice != 2, ConversionChoice != 1)
    	{cout << "What do you think you're doing? None of the conversion was chosen!" << endl;
    	}
    
    	return 0;
    }
    I was able to do this....

    For the question that I was struggling with, I think I could do something like tis:
    Code:
    #include <iostream.h>
    
    main ()
    {
    	int name;
    
    	cout << "Choose a family member:";
    	cout << "Press '1' for [name1]";
    	cout << "Press '2' for [name2]";
    	cout << "Press '3' for [name3]";
    	cout << ....
    	....
    	....etc.
    	cin >> name;
    
    	if (name = 1);
    	{cout << "Relation: Father";
    	cout << "Height: bla bla bla";
    	cout << "weight: bla blab la...;
    	cout << ....
    	....
    	.....etc ... << endl;
    	}
    
    	if (name = 2);
    	{cout << "Relation: Mother";
    	cout << "Height: bla bla bla";
    	cout << "weight: bla blab la...;
    	cout << ....
    	....
    	.....etc ... << endl;
    	}
    
    
    	else if ( name != 1, name != 2, name != 3, ....etc.... );
    	{cout << "invalid number.. bla blab la" << endl;
    
    	return 0;
    }
    But this way im using integers to represent the family members. I don't know how to do something similar but using characters instead of integers.

    I don't know wheter this would be breaking ther forum rules, but could someone show me an example of using if for words?... I'll figure out the rest by myself

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    > if (name = 1);
    1. Use == for comparison
    2. the lone ; at the end means this if statement does nothing.

    To compare strings in C++, do something like
    Code:
    #include <string>
    
    
    // then in code
    std::string s = "hello";
    if ( s == "hello" ) {
      std::cout << "yes";
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    You may have just saved my life.... I will test it tommorow morning just as I get my hands on C++ in the com lab.

    Thank you all. Bless this forum!!! I've gotta go to bed now!!..

  10. #10
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Okay, I'm already at the com lab... Thanks guys.

    I mannage to get this to work.
    Code:
    #include <iostream>
    #include <string>
    
    main ()
    {
    	char *s;
    	std::cout << "type 'hello': ";
    	std::cin >> *s;
    	if ( s == "hello" );
    	std::cout << "yes ";
    
    	return 0;
    }
    Thanks for that.... But I can't get this to work.
    Code:
    #include <iostream>
    #include <string>
    
    main ()
    {
    	char *s;
    	std::cout << "type 'hello': ";
    	std::cin >> *s;
    	if ( s == "hello" );
    	std::cout << "yes ";
    	else std::cout << "no";
    
    	return 0;
    }
    this is the error:
    c:\windows\system32\teste.cpp(10) : warning C4390: ';' : empty controlled statement found; is this the intent?

    Why ist this? the else doesn't work...

  11. #11
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    That is some of the most frightening code I've ever seen. Your pointer is pointing off into the oblivion as you merrily write data to it. Your comparison isn't using strcmp. Are you not allowed to use strings?
    Code:
    #include <iostream>
    #include <string>
    
    main ()
    {
    	std::string s;
    	std::cout << "type 'hello': ";
    	std::cin >> s;
    	if ( s == "hello" );
    		std::cout << "yes ";
    
    	return 0;
    }
    The logical problem with both sets of code is the semi-colon at the end of your if statement. Get rid of it.
    Last edited by pianorain; 10-19-2006 at 08:21 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  12. #12
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    If I were you, I wouldn't worry about using pointers to character arrays. I would just use strings.

    Someone posted this here awhile ago and I think it might help you:

    Code:
    // An improved version of the program! ^_^ Good mood...
    #include <iostream>
    
    #include <string>
    
    
    int main (void) 
    {
       std::string answer = "twenty";
       std::cout << "How many apples did I buy? (word answers):";
       std::cin >> answer;
       // This dandy little algorithm transforms a string based on the
       // predicate. I turned everything lower case to make sure it 
       // matches.
       transform(answer.begin(), answer.end(), answer.begin(), ::tolower);
    
       if(answer != "twenty")
       {
           std::cout << "Sorry, you're wrong\n";
       }
       else 
       {
           std::cout << "Yes, that's correct\n";
       }
       
       
       std::cin.ignore();
       std::cin.get();
    }

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Using char arrays:
    Code:
    #include <iostream>
    
    int main(){
        char s[256];
        std::cout << "Type 'hello': ";
        std::cin.getline(s,256,'\n');
        if (strcmp(s,hello)==0){
            std::cout << "You typed 'hello'!";
        }
        else{
            std::cout << "You didn't type 'hello'.";
        }
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        return 0;
    }
    Using strings:
    Code:
    #include <iostream>
    #include <string>
    
    int main(){
        std::string s;
        std::cout << "Type 'hello': ";
        std::cin >> s;
        if(s=="hello"){
            std::cout << "You typed 'hello'!";
        }
        else{
            std::cout << "You didn't type 'hello'.";
        }
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        return 0;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #14
    Registered User [Z-D]'s Avatar
    Join Date
    Oct 2006
    Posts
    37
    Ohhh...... Thanks you guys. That's a lot of help.... This forum really saved me...

    Quote Originally Posted by pianorain
    That is some of the most frightening code I've ever seen. Your pointer is pointing off into the oblivion as you merrily write data to it. Your comparison isn't using strcmp. Are you not allowed to use strings?
    The problem is I don't know much about strings, char and arrays, I haven't learn them... So by looking at these posts, I understand them better and I now know that I should use stirngs.

    Quote Originally Posted by Loctan
    If I were you, I wouldn't worry about using pointers to character arrays. I would just use strings.

    Someone posted this here awhile ago and I think it might help you:

    Code:
    // An improved version of the program! ^_^ Good mood...
    #include <iostream>
    
    #include <string>
    
    
    int main (void) 
    {
       std::string answer = "twenty";
       std::cout << "How many apples did I buy? (word answers):";
       std::cin >> answer;
       // This dandy little algorithm transforms a string based on the
       // predicate. I turned everything lower case to make sure it 
       // matches.
       transform(answer.begin(), answer.end(), answer.begin(), ::tolower);
    
       if(answer != "twenty")
       {
           std::cout << "Sorry, you're wrong\n";
       }
       else 
       {
           std::cout << "Yes, that's correct\n";
       }
       
       
       std::cin.ignore();
       std::cin.get();
    }
    Yeah this helped, Glad to see you in a good mood. But I don't understand the one I coloured red.

    Quote Originally Posted by maxorator
    Using strings:
    Code:
    #include <iostream>
    #include <string>
    
    int main(){
        std::string s;
        std::cout << "Type 'hello': ";
        std::cin >> s;
        if(s=="hello"){
            std::cout << "You typed 'hello'!";
        }
        else{
            std::cout << "You didn't type 'hello'.";
        }
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        return 0;
    }
    This one seems to be esier for me to understand, and more suitable for my question. Because the question needs me to write a programme where if a name, out of a given choice is input-ed, certain data will be output-ed.
    Also I don't under stand this.


    So... would this work!?...
    Code:
    #include <iostream>
    #include <string>
    
    int main(){
        std::string s;
        std::cout << "Type in one of the names given below: ";
        std::cout << "'name1', 'name2', 'name3', 'name4'";
        std::cin >> s;
        if(s=="name1")
        {
            std::cout << "Relation: Father";
    	std::cout << "Height: ...";
    	std::cout << "weight: ...";
    	std::cout << "Hair: ...";
        }
    
        if(s=="name2")
        {
            std::cout << "Relation: Mother";
    	std::cout << "Height: ...";
    	std::cout << "weight: ...";
    	std::cout << "Hair: ...";
        }
    
        if(s=="name3")
        {
            std::cout << "Relation: Sister";
    	std::cout << "Height: ...";
    	std::cout << "weight: ...";
    	std::cout << "Hair: ...";
        }
    
        if(s=="name4")
        {
            std::cout << "Relation: Brother";
    	std::cout << "Height: ...";
    	std::cout << "weight: ...";
    	std::cout << "Hair: ...";
        }
        else{
            std::cout << "The name typed in isn't a family member";
        }
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        
        return 0;
    }
    The red one was taken from the above post.... I just use it but i actually don't know what it does... Could someone pleas explain. But I'll read up on it anyway.

    Sorry, but again, I'm at home. No C++. So I can't check whether it's working. Right now I will be having a holiday so my next class will be on next thursday(26th) and friday(but I won't have the time to use the lab on friday) and I ive labout an hour away from my universtity... So I was planning to get the correct code before then, and test it there and then and complete it before I have to pass it up. Which is on the following tuesday. Again sapperated by week ends.


    So, if I'm not being a niusance and if any of you don't mind.... could you please test my code?....

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    std::numeric_limits<std::streamsize>::max() is equal to the maximum value of a variable of type std::streamsize.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21