Thread: Please help me with this string

  1. #1
    Unregistered
    Guest

    Question Please help me with this string

    Hello.
    I am extremely new to Programming, but I am trying to make my first littl program.
    And so, could you PLEASE tell me what is wrong with this string?



    #include <stdio.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <iostream.h>

    void main(void)
    {
    typedef char fred(40);
    fred="Hello";
    cout<<fred;
    }



    It brings up about 6 errors at last count.
    All I want is something to store a word, in this case "hello". I think it might have some thing to do with the fact that "hello" is not 40 characters, but I have tried 4, 5, 6, 7, 8+ numbers and it still dosent seem to work (Besides my help file tells me that I can say 40 and only use 10).

    If you recognise what is wrong here could you please tell me? Thanks in advance.

    "Do something nice for others. That way they owe you in return" -My Mum

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    how 'bout this

    Code:
    char fred[] = "Hello";

  3. #3
    Unregistered
    Guest
    Thanks for what you said, but im afraid I didnt say what I actually wanted the program to do, this is what I wanted:


    #include <stdio.h>
    #include <stdlib.h>
    #include <fstream.h>
    #include <iostream.h>


    char username[40];


    void main(void)
    {
    cout<<"Write your name:";
    cin>>username;
    cout<<"Your user name was: " << username;
    }


    The program you wrote only works if I decalre the value of the variable at the same time as I am stating it, but I need the variable able to allways be different. Sorry, my mistake!

    "Do something nice for others. That way they owe you in return" - My Mum

  4. #4
    Unregistered
    Guest
    #include <iostream>

    using namespace std;

    int main()
    {
    char uName[15];

    cout << "Hello, what is your username?: ";
    cin >> uName;
    cout << "Your username is: " << uName << endl;

    return 0;
    }

  5. #5
    Unregistered
    Guest
    NOte that the newer (prefered if your compiler is compliant) style involves using namespaces and int main() rather than void main().

    Also note that the >> will only accept strings that don't contain spaces (or other white spaces for that matter). Therefore if the user enters:

    al quedah

    only al will be stored in the array. The remainder will be ignored (end of political comment). There are ways to store strings that contain white space, but that will come later in your education.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM