Thread: n00b Code Error.

  1. #1
    #junkie
    Join Date
    Oct 2004
    Posts
    240

    Post n00b Code Error.

    Im reading tutorials and whatnot, and im trying to get used to creating my own functions.

    Now this may not be a function error, i dunno. But until i added the function sections i was doing fine. Basically a tutorial gave me "homework" so i tried to do it, and failed . What im sposed to do is make a program to have the user input a number between 1 and 20, and once inputted the program uses if statements to check if its between 1 and 10, or between 11 and 20. The first if will use a function that just messages the user, the other function takes the users input and adds 35 to it.

    Thanks for any help and forgive my n00b ignorance lol. Also keep note my coding "style" is none existant seeing as im learning from whatever tutorial explains the topic at hand in a way i get best.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void ztot()
    {
        cout<<"You number was between 1 and 10."<<endl;
    }
    void ttot(int y)
    {
        int c = y + 35;
        cout<<y<<" was your number, "<<y<<" + 35 = "<<c<<"."endl;
    }    
    int main()
    {
        string T = "Please type a Number between 1 and 20";
        cout<<T<<endl;
        int x, y;
        while (!(cin>>x)){
        cout<<"You can only use numbers for this."<<endl;
        cin.clear();
        cin.ignore(1000,'\n');
        cout<<T<<endl;
        }   
        while((x < 1) || (x > 20)){
            cout<<"You have inputed a number not in the range of 1-20."<<endl;
            cin.clear();
            cin.ignore(1000,'\n');
            cout<<T<<endl;
        }
        if(x <= 10){
            ztot();
        }else{
            ttot(x);
        }    
    //    cout<<x<<endl;
        cin.ignore();
        cin.get();
    }
    Error:
    helloworld.cpp: In function `void ttot(int)':
    helloworld.cpp:12: error: syntax error before `;' token

    make.exe: *** [helloworld.o] Error 1

    Execution terminated
    Side note, is there anything wrong with anything im using? Such as void, endl, the cin.ignore/get combo. ect. I dont fully comprehend the whole flushing and needing to ignore. Same with in the while loops how you clear, which in my description says it clears all text, then you gatta use something again to clearn the text "cin.ignore(1000,'\n');". Anyway, thanks for the help
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You are missing a "<<". Change your line
    Code:
        cout<<y<<" was your number, "<<y<<" + 35 = "<<c<<"."endl;
    to
    Code:
        cout<<y<<" was your number, "<<y<<" + 35 = "<<c<<"."<<endl;

  3. #3
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    ah ty, sorry it was a lame simple one. Figured it was a full scripting error not oversight. I tripple checked though so sorry again lol. Ty though
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    This code needs some changes:
    Code:
        while (!(cin>>x)){
            cout<<"You can only use numbers for this."<<endl;
            cin.clear();
            cin.ignore(1000,'\n');
            cout<<T<<endl;
        }   
        while((x < 1) || (x > 20)){
            cout<<"You have inputed a number not in the range of 1-20."<<endl;
            cin.clear();
            cin.ignore(1000,'\n');
            cout<<T<<endl;
        }
    Technically, those loops should be combined into a single while loop because the point of that while loop would be to run until the user entered valid input. So maybe add the condition from the second while loop into the first, and then use an if statement to provide the appropriate error message.

    Also, a minor detail is your variable and function names. They confused me because they aren't descriptive in any way. I know this is just a test program that does nothing, but I'm sure you can come up with a better variable name than T.

  5. #5
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    that however is my style overall. as with temp variables in my old "language", unless the variables are perm, or like functions, i tend to shorten everything lol. ie "ttot" stands for ten to twenty, ztot stands for zero to ten. Stuff like that is somethin i always do.

    Sorry lol
    Oh and on the while loops, first off the seperate cuz of habit again, my old language i believe does not allow nested loops. So inturn im using to sperating them, itll be one thing i gatta remember lol.

    And also the 2nd loop (if i remember right) cannot be broken because it does not redefine x, so i put a cin>>x; in there to allow the user to break it.

    Thanks for your insites
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  4. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM